-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxgbmult.py
289 lines (217 loc) · 8.55 KB
/
xgbmult.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
import xgboost as xgb
import datetime
TODAY = datetime.datetime.today()
print "today's date is ", TODAY.strftime("%m%d")
STOPPING = 200
BOOST = 50000
## Start of main script
train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")
#ids=test[['PLAYER_ID','PLAYER_NAME']]
train.fillna(-1,inplace=True, downcast = 'infer')
test.fillna(-1,inplace=True, downcast = 'infer')
features_train = train.columns[51:56]
features_test = test.columns[-5:]
team_dummies = [c for c in test.columns.values if 'Opponent_' in c]
player_dummies = [c for c in test.columns.values if 'PLAYER_ID_' in c]
position_dummies = [c for c in test.columns.values if 'Position_' in c ]
player_dummies.remove('PLAYER_ID_0') # this shows up in test but not in train
other14_features = test.columns.values[26:46]
train.MIN = train.MIN.replace(0,0.5)
train.FanDuel = train.FanDuel.fillna(0.0)
train['fd_per_minute'] = train.FanDuel.divide(train.MIN,fill_value = 0.0)
features_test = team_dummies+player_dummies+position_dummies+['home','away','rest','recent_minutes','AGE']+list(other14_features)
features_train =features_test
features = features_train
def rmspe(y, yhat):
print (y.min())
s = pd.Series(y,yhat)
ret = np.sqrt(np.mean(((y - yhat)) ** 2))
#print(s)
return ret
def rmspe_xg(yhat, y):
y = y.get_label()
return "rmspe", rmspe(y, yhat)
## STEP 1 is the classic, more straightforward one.
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.003,
"max_depth": 9,
"subsample": 0.85,
"colsample_bytree": 0.4,
"min_child_weight": 4,
"silent": 1,
"thread": 1,
"seed": 18904
}
num_boost_round = BOOST
X_train, X_valid = train_test_split(train, test_size=0.12, random_state=15)
y_train = X_train.FanDuel
y_valid = X_valid.FanDuel
dtrain = xgb.DMatrix(X_train[features_train], y_train)
dvalid = xgb.DMatrix(X_valid[features_train], y_valid)
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=STOPPING, \
feval=rmspe_xg, verbose_eval=True)
print("Validating")
yhat = gbm.predict(xgb.DMatrix(X_valid[features_train]))
error = rmspe(X_valid.FanDuel.values, yhat)
print('RMSPE: {:.6f}'.format(error))
print("Make predictions on the test set")
dtest = xgb.DMatrix(test[features_test])
test_probs = gbm.predict(dtest)
result = pd.DataFrame({"Id": test["Id"], 'FanDuel1': test_probs, 'First Name': test['First Name'], 'Last Name' : test['Last Name'], 'FPPG_historical' : test['FPPG'], 'Salary': test['Salary'], 'Team': test['Team']})
# STEP two, we predict minutes
## first expected FanDuel per minute ######################
X_train, X_valid = train_test_split(train, test_size=0.12, random_state=12)
y_train = X_train.fd_per_minute
y_valid = X_valid.fd_per_minute
print(len(y_valid))
dtrain = xgb.DMatrix(X_train[features_train], y_train)
dvalid = xgb.DMatrix(X_valid[features_train], y_valid)
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=STOPPING, \
feval=rmspe_xg, verbose_eval=True)
yhat = gbm.predict(xgb.DMatrix(X_valid[features_train]))
error = rmspe(X_valid.fd_per_minute.values, yhat)
print('RMSPE: {:.6f}'.format(error))
dtest = xgb.DMatrix(test[features_test])
test_probs = gbm.predict(dtest)
result['fd_per_minute']= test_probs
## next expected minutes ######################
X_train, X_valid = train_test_split(train, test_size=0.12, random_state=12)
y_train = X_train.MIN
y_valid = X_valid.MIN
print(len(y_valid))
dtrain = xgb.DMatrix(X_train[features_train], y_train)
dvalid = xgb.DMatrix(X_valid[features_train], y_valid)
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=STOPPING, \
feval=rmspe_xg, verbose_eval=True)
yhat = gbm.predict(xgb.DMatrix(X_valid[features_train]))
error = rmspe(X_valid.MIN.values, yhat)
print('RMSPE: {:.6f}'.format(error))
dtest = xgb.DMatrix(test[features_test])
test_probs = gbm.predict(dtest)
result['MINexpected'] =test_probs
result['FDexpected'] = result.MINexpected*result.fd_per_minute
error = rmspe(X_valid.MIN.values, yhat)
print (result.head(10))
#Next, stats separately
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.008,
"max_depth": 9,
"subsample": 0.85,
"colsample_bytree": 0.4,
"min_child_weight": 5,
"silent": 1,
"thread": 1,
"seed": 11126
}
num_boost_round = BOOST
statlist = ['FGM', 'FG3M', 'FTM','REB','AST','BLK','STL','TOV']
def stattrain(s):
print("Train a XGBoost model", s)
X_train, X_valid = train_test_split(train, test_size=0.14, random_state=10)
y_train = X_train[s].fillna(0)
print (y_train)
y_valid = X_valid[s].fillna(0)
dtrain = xgb.DMatrix(X_train[features], y_train)
dvalid = xgb.DMatrix(X_valid[features], y_valid)
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=STOPPING, feval=rmspe_xg, verbose_eval=True)
print("Validating")
yhat = gbm.predict(xgb.DMatrix(X_valid[features]))
error = rmspe(X_valid.FanDuel.values, yhat)
print('RMSPE: {:.6f}'.format(error))
print("Make predictions on the test set")
dtest = xgb.DMatrix(test[features])
test_probs = gbm.predict(dtest)
result = pd.DataFrame({"PLAYER_ID": test["PLAYER_ID"], s: test_probs})
return result
ids=test[['PLAYER_ID','Last Name']]
pred_df = ids
for s in statlist:
a= stattrain(s)
pred_df = pd.merge(pred_df, a, on ='PLAYER_ID',how = 'left')
pred_df ['FanDuel']=2*pred_df ['FGM']+pred_df ['FG3M']+pred_df ['FTM']+1.2*pred_df ['REB']+1.5*pred_df ['AST']+2*pred_df ['BLK']+2*pred_df ['STL']-pred_df ['TOV']
result['statspred']=pred_df ['FanDuel']
# finally, we do a lower and a higher pred
def rmspeW(y, yhat, PLUS, MINUS):
diff = y - yhat
over = np.maximum(diff, 0)
under = np.maximum(-diff, 0)
weighted = PLUS*over + MINUS*under
ret = np.sqrt(np.mean((weighted) ** 2))
return ret
def rmspe_xgH(yhat, y):
y = y.get_label()
return "rmspe", rmspeW(y, yhat, 1, 1.7)
def rmspe_xgL(yhat, y):
y = y.get_label()
return "rmspe", rmspeW(y, yhat, 1.7, 1.0)
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.008,
"max_depth": 9,
"subsample": 0.85,
"colsample_bytree": 0.4,
"min_child_weight": 5,
"silent": 1,
"thread": 1,
"seed": 19726
}
num_boost_round = BOOST
X_train, X_valid = train_test_split(train, test_size=0.14, random_state=9)
y_train = X_train.FanDuel
y_valid = X_valid.FanDuel
print(len(y_valid))
dtrain = xgb.DMatrix(X_train[features_train], y_train)
dvalid = xgb.DMatrix(X_valid[features_train], y_valid)
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=STOPPING, \
feval=rmspe_xgH, verbose_eval=True)
print("Validating")
yhat = gbm.predict(xgb.DMatrix(X_valid[features_train]))
error = rmspeW(X_valid.FanDuel.values, yhat, 1.0, 1.7)
print('RMSPE: {:.6f}'.format(error))
print("Make predictions on the test set")
dtest = xgb.DMatrix(test[features_test])
test_probs = gbm.predict(dtest)
result['punishlower'] = test_probs
## And the other side
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.008,
"max_depth": 9,
"subsample": 0.85,
"colsample_bytree": 0.4,
"min_child_weight": 5,
"silent": 1,
"thread": 1,
"seed": 16
}
num_boost_round = BOOST
X_train, X_valid = train_test_split(train, test_size=0.14, random_state=11)
y_train = X_train.FanDuel
y_valid = X_valid.FanDuel
print(len(y_valid))
dtrain = xgb.DMatrix(X_train[features_train], y_train)
dvalid = xgb.DMatrix(X_valid[features_train], y_valid)
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=STOPPING, \
feval=rmspe_xgL, verbose_eval=True)
print("Validating")
yhat = gbm.predict(xgb.DMatrix(X_valid[features_train]))
error = rmspeW(X_valid.FanDuel.values, yhat, 1.0, 1.7)
print('RMSPE: {:.6f}'.format(error))
print("Make predictions on the test set")
dtest = xgb.DMatrix(test[features_test])
test_probs = gbm.predict(dtest)
result['punishhigher'] = test_probs
filename = TODAY.strftime("%m%d")+'.csv'
result.to_csv(filename)