Repository navigation
Expand file tree
/
Copy pathrandom_forest_regressor.py
More file actions
477 lines (390 loc) · 19.9 KB
/
Copy pathrandom_forest_regressor.py
File metadata and controls
477 lines (390 loc) · 19.9 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import pandas as pd
import datetime
import numpy as np
from sklearn import preprocessing, cross_validation, svm, linear_model
import matplotlib.pyplot as plt
from sklearn.learning_curve import learning_curve
from sklearn.feature_selection import RFE, RFECV
from sklearn.ensemble import RandomForestRegressor
import os
import pickle
os.system('clear')
"""Found at http://scikit-learn.org/stable/auto_examples/model_selection/plot_learning_curve.html"""
def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None, n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
plt.figure()
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = learning_curve(
estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
plt.grid()
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
label="Cross-validation score")
plt.legend(loc="best")
return plt
"""This method is purely for checking that the logic works"""
def print_full(x):
pd.set_option('display.max_rows', len(x))
print(x)
pd.reset_option('display.max_rows')
"""This method converts the calculated probability to a spread"""
def spread_conversion(x):
"""
http://www.bettingtalk.com/win-probability-percentage-point-spread-nfl-nba/
"""
home_dog = False
if x < 0.5:
x = 1.0 - x
home_dog = True
if x < .513:
return_val = -0.5
elif x < .525:
return_val = -1
elif x < .535:
return_val = -1.5
elif x < .545:
return_val = -2
elif x < .594:
return_val = -2.5
elif x < .643:
return_val = -3
elif x < .658:
return_val = -3.5
elif x < .673:
return_val = -4
elif x < .681:
return_val = -4.5
elif x < .69:
return_val = -5
elif x < .707:
return_val = -5.5
elif x < .724:
return_val = -6
elif x < .752:
return_val = -6.5
elif x < .781:
return_val = -7
elif x < .791:
return_val = -7.5
elif x < .802:
return_val = -8
elif x < .807:
return_val = -8.5
elif x < .811:
return_val = -9
elif x < .836:
return_val = -9.5
elif x < .86:
return_val = -10
elif x < .871:
return_val = -10.5
elif x < .882:
return_val = -11
elif x < .885:
return_val = -11.5
elif x < .887:
return_val = -12
elif x < .893:
return_val = -12.5
elif x < .9:
return_val = -13
elif x < .924:
return_val = -13.5
elif x < .949:
return_val = -14
elif x < .956:
return_val = -14.5
elif x < .963:
return_val = -15
elif x < .981:
return_val = -15.5
elif x < .998:
return_val = -16
else:
return_val = -16.5
if home_dog == True:
return float(-return_val)
else:
return float(return_val)
C_vec = [0.0001, 0.0003, 0.0006, 0.001, 0.003, 0.006, 0.01, 0.03, 0.06, 0.1, 0.3, 0.6, 1.0, 3.0, 6.0, 10.0]
prob_val = 0
C_val = 0
# Read in csv table
df = pd.read_csv('nflscraper/command_results.csv')
# Calculate offensive efficiency (pass yards per attempt and rush yards per carry)
home_pass_stats = pd.DataFrame(df['hpass_tot'].str.split('-').tolist(),columns="completions attempts yards touchdowns interceptions".split())
away_pass_stats = pd.DataFrame(df['apass_tot'].str.split('-').tolist(),columns="completions attempts yards touchdowns interceptions".split())
away_pass_stats.replace('neg7','-7',inplace=True)
home_pass_stats['yards'] = home_pass_stats['yards'].astype(float)
home_pass_stats['attempts'] = home_pass_stats['attempts'].astype(float)
away_pass_stats['yards'] = away_pass_stats.yards.astype(float)
away_pass_stats['attempts'] = away_pass_stats.attempts.astype(float)
df['home_pass'] = home_pass_stats['yards'] / home_pass_stats['attempts']
df['away_pass'] = away_pass_stats['yards'] / away_pass_stats['attempts']
del home_pass_stats, away_pass_stats
df['home_rush'].astype(float)
df['hrush_att'].astype(float)
df['away_rush'].astype(float)
df['arush_att'].astype(float)
df['home_rush'] = df['home_rush'] / df['hrush_att']
df['away_rush'] = df['away_rush'] / df['arush_att']
# Change the string date data in df to datetime format
df['game_date'] = pd.to_datetime(df['game_date'],format='%Y-%m-%d')
""" Create home time of possession differential """
# Convert objects to datetime values
df['home_poss'] = pd.to_datetime(df['home_poss'],format='%M:%S')
df['away_poss'] = pd.to_datetime(df['away_poss'],format='%M:%S')
# Convert datetime values to fractions of an hour
df['home_poss'] = df['home_poss'].dt.minute / 60.0 + df['home_poss'].dt.second / 3600.0
df['away_poss'] = df['away_poss'].dt.minute / 60.0 + df['away_poss'].dt.second / 3600.0
# Find total possession time (only really matters because games can go to overtime)
# And re-weight time of possession
df['total_poss'] = df['home_poss'] + df['away_poss']
df['home_poss'] = df['home_poss'] / df['total_poss']
df['away_poss'] = df['away_poss'] / df['total_poss']
df.drop('total_poss',axis = 1, inplace=True) # Delete total possession column as it's no longer needed
""" Set date after which you don't want to use data to train classifier """
cutoff_date = datetime.date(2015, 8, 1)
""" Creating prediction dataset """
# Create dataset to be used for prediction
predicting_set = df[df.game_date >= cutoff_date].set_index('game_date')
predicting_set = predicting_set.sort_index() # Sort ascending by index
predicting_set['week'] = np.nan # Create column indicating which game week the team is in
# Populating the week column of predicting_set
start_date = predicting_set.index.min().date() # Find the first game date
end_date = predicting_set.index.max().date() # Find the last game date
date_val = start_date # date_val is the value used to cycle through the datetime objects
week_val = 1 # week_val is the iterating value that tracks the "current" week
week_dict={} # Create an empty dictionary
# Cycle through the number of days
for _ in range((end_date - start_date).days):
if date_val.weekday() == 1: # Check if the weekday value is a Tuesday
week_val += 1 # Increment since I'm considering Tuesday to start a new week
week_dict[date_val] = week_val # Update the dictionary value for the "current" date
date_val += datetime.timedelta(days=1) # Increment date_val by one day
week_dict[datetime.date(2016,2,7)] = 21 # This is manually done since there are two weeks between conf championships and the final game
# Update the week column in the dataframe with the dictionary values and then delete the dictionary and its components used to create it
predicting_set['week'].update(pd.Series(week_dict))
predicting_set['vegasline'].replace('Pick','0',inplace=True)
del week_dict, start_date, end_date, date_val, week_val
# Manually call the names of the columns from the scraped data
home_columns = ['home_four','home_oyds','home_pass','home_pens','home_poss','home_rush','home_sack','home_score','home_team','home_third','home_turn','hpens_yds','hsack_yds','hpass_tot','hrush_att']
away_columns = ['apens_yds','asack_yds','away_four','away_oyds','away_pass','away_pens','away_poss','away_rush','away_sack','away_score','away_team','away_third','away_turn','apass_tot','arush_att']
# Create a mapping to combine home and away columns
home_cols = {'game week': 'game week', 'home_four': 'fourth down', 'home_oyds': 'total yards', 'home_pass': 'pass yards', 'home_pens': 'penalties', 'home_poss': 'possession', 'home_rush': 'rush yards',
'home_sack': 'sacks', 'home_score': 'score', 'home_team': 'team', 'home_third': 'third down', 'home_turn': 'turnovers', 'hpens_yds': 'penalty yards', 'hsack_yds': 'sack yards',
'vegasline': 'spread', 'overunder': 'total score'}
away_cols = {'game week': 'game week', 'away_four': 'fourth down', 'away_oyds': 'total yards', 'away_pass': 'pass yards', 'away_pens': 'penalties', 'away_poss': 'possession', 'away_rush': 'rush yards',
'away_sack': 'sacks', 'away_score': 'score', 'away_team': 'team', 'away_third': 'third down', 'away_turn': 'turnovers', 'apens_yds': 'penalty yards', 'asack_yds': 'sack yards',
'vegasline': 'spread', 'overunder': 'total score'}
# Create only home and away dataframes
away = predicting_set.drop(home_columns,axis=1)
home = predicting_set.drop(away_columns,axis=1)
away.drop('apass_tot',axis=1,inplace=True)
away.drop('arush_att',axis=1,inplace=True)
away.drop('away_oyds',axis=1,inplace=True)
home.drop('hpass_tot',axis=1,inplace=True)
home.drop('hrush_att',axis=1,inplace=True)
home.drop('home_oyds',axis=1,inplace=True)
# Create home and away scores which will be used to compare to the predicted value
away_score = predicting_set[['away_score','week']]
home_score = predicting_set[['home_score','week']]
# Remove all games not included in the prediction
away_score = away_score[away_score.week >= 5]
home_score = home_score[home_score.week >= 5]
# Drop the 'week' column as it is no longer needed
away_score.drop('week',axis=1,inplace=True)
home_score.drop('week',axis=1,inplace=True)
# Rename the columns in these dataframes removing the home and away modifier according to the mapping
away.rename(columns=away_cols,inplace=True)
home.rename(columns=home_cols,inplace=True)
# Sort the rows chronologically
away.sort_index(axis=1,inplace=True)
home.sort_index(axis=1,inplace=True)
away_score.sort_index(inplace=True)
home_score.sort_index(inplace=True)
# Pull the actual spreads from the scraped data
spreads = home[home['week'] >= 5]
spreads = spreads['spread'].str.split().str[-1]
spreads = pd.to_numeric(spreads)
home.drop(['spread','total score'],axis=1,inplace=True)
away.drop(['spread','total score'],axis=1,inplace=True)
# Create the actual result vector where a tie counts as a loss for the home team
game_result = np.array(np.where(home_score.ix[:,0] + spreads[:] > away_score.ix[:,0], 1, 0))
# Group both home and away stats into one dataframe
total_set = home.append(away)
# Create a list of the 32 teams
team_list = total_set['team'].unique()
# This loop will pull in all data and calculate running averages
for week in range(5,22):
# Initialize a "temporary" dataframe for each loop
weekly_stats = pd.DataFrame(columns=total_set.columns.values)
# Iterate through each team in the list
for team in team_list:
# Pull data only for the team being addressed in each iteration
mask_team = total_set[total_set['team'] == team]
# Pull data only for the weeks of interest
mask_week = mask_team[mask_team['week'] < week]
# Append the "temporary" dataframe with this pulled data
weekly_stats = weekly_stats.append(mask_week)
# Calculate the mean of all data for each team
weekly_stats = weekly_stats.groupby('team').mean()
# Reset the week value to equal the week that will be used (for example, week 5 will have all averaged data from weeks 1-4)
weekly_stats['week'] = week
# The "total_stats" set is created from weekly_stats in order to have the same column values, and only needs to be done for the first week
if week == 5:
total_stats = weekly_stats
# For all other weeks simply append the "temporary" values
else:
total_stats = total_stats.append(weekly_stats)
# Create the needed columns for the eventual prediction
matchup_columns = ['week','home_team','away_team', 'sack_diff', 'sack_ydiff', 'pens_diff', 'poss_diff', 'third_diff', 'turn_diff', 'pass_diff', 'rush_diff', 'fourth_diff']
# matchup_columns = ['week','home_team','away_team', 'poss_diff', 'third_diff', 'turn_diff', 'pass_diff', 'rush_diff']
# Create the DataFrame and pull in the 'week' 'home_team' and 'away_team columns
matchups = pd.DataFrame(columns = matchup_columns)
matchups[['week','home_team','away_team']] = predicting_set[['week','home_team','away_team']]
# Remove any results from the first four weeks
matchups = matchups[matchups.week >= 5]
# Create the actual differential values using averages from each week
for row in range(len(matchups)):
h_team = matchups.iloc[row]['home_team']
a_team = matchups.iloc[row]['away_team']
week = matchups.iloc[row]['week']
# pass_diff
h_pass = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['pass yards'].values[0]
a_pass = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['pass yards'].values[0]
matchups.ix[row,'pass_diff'] = h_pass - a_pass
# rush_diff
h_rush = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['rush yards'].values[0]
a_rush = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['rush yards'].values[0]
matchups.ix[row, 'rush_diff'] = h_rush - a_rush
# sack_diff
h_sack = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['sacks'].values[0]
a_sack = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['sacks'].values[0]
matchups.ix[row, 'sack_diff'] = h_sack - a_sack
#sack_ydiff
h_sack_yds = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['sack yards'].values[0]
a_sack_yds = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['sack yards'].values[0]
matchups.ix[row, 'sack_ydiff'] = h_sack_yds - a_sack_yds
# pens_diff
h_pen_yds = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['penalty yards'].values[0]
a_pen_yds = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['penalty yards'].values[0]
matchups.ix[row, 'pens_diff'] = h_pen_yds - a_pen_yds
# poss_diff
h_poss = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['possession'].values[0]
a_poss = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['possession'].values[0]
matchups.ix[row, 'poss_diff'] = h_poss - a_poss
# third_diff
h_third = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['third down'].values[0]
a_third = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['third down'].values[0]
matchups.ix[row, 'third_diff'] = h_third - a_third
# fourth_diff
h_fourth = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['fourth down'].values[0]
a_fourth = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['fourth down'].values[0]
matchups.ix[row, 'fourth_diff'] = h_fourth - a_fourth
# turn_diff
h_turn = total_stats[((total_stats.index.values == h_team) & (total_stats.week == week))]['turnovers'].values[0]
a_turn = total_stats[((total_stats.index.values == a_team) & (total_stats.week == week))]['turnovers'].values[0]
matchups.ix[row, 'turn_diff'] = h_turn - a_turn
""" Create the testing set for the algo creation """
# Remove the predicting set from the dataframe
df = df[df.game_date < cutoff_date]
# Fill NaNs with outlier values
df.fillna(-999999, inplace=True)
# Calculate time of possession differential
df['poss_diff'] = df['home_poss'] - df['away_poss']
# Calculate third down percentage differential
df['third_diff'] = df['home_third'] - df['away_third']
# Calculate third down percentage differential
df['fourth_diff'] = df['home_four'] - df['away_four']
# Calculate turnover differential
df['turn_diff'] = df['home_turn'] - df['away_turn']
# Calculate sack quantity differential
df['sack_diff'] = df['home_sack'] - df['away_sack']
# Calculate sack yards differential
df['sack_ydiff'] = df['hsack_yds'] - df['asack_yds']
# Calculate penalty yards differential
df['pens_diff'] = df['hpens_yds'] - df['apens_yds']
# Calculate passing yardage differential
df['pass_diff'] = df['home_pass'] - df['away_pass']
# Calculate rushing yardage differential
df['rush_diff'] = df['home_rush'] - df['away_rush']
# Create a sample set to pass into the machine learning algorithm
X = df[['sack_diff', 'sack_ydiff', 'pens_diff', 'poss_diff', 'third_diff', 'turn_diff', 'pass_diff', 'rush_diff', 'fourth_diff']].copy()
# X = df[['poss_diff', 'third_diff', 'turn_diff', 'pass_diff', 'rush_diff']].copy()
# Create results vector (a home win = 1, a home loss or tie = 0)
y = np.array(np.where(df['home_score'] > df['away_score'], 1, 0))
# title = "Learning Curves (Simple Regression)"#, RBF kernel, $\gamma=0.001$)"
# # SVC is more expensive so we do a lower number of CV iterations:
# cv = cross_validation.ShuffleSplit(X.shape[0], n_iter=100,
# test_size=0.2, random_state=0)
# estimator = linear_model.LogisticRegression(C=0.003)
# plot_learning_curve(estimator, title, X, y, cv=cv, n_jobs=4)
# plt.show()
""" Train, test, and predict the algorithm """
# Scale the sample data
scaler = preprocessing.StandardScaler().fit(X)
X = scaler.transform(X)
# Delete the dataframe to clear memory
del df
# Split out training and testing data sets
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X,y,test_size=0.2)
# Remove the 'week' 'home_team' and 'away_team' columns from matchups as they are not used in the algorithm
matchups.drop(['week', 'home_team', 'away_team'], axis=1, inplace=True)
rf = RandomForestRegressor(n_estimators=500)
rf.fit(X_train,y_train)
print rf.score(X_test,y_test)
probabilities = rf.predict(scaler.transform(matchups))
vfunc = np.vectorize(spread_conversion)
predicted_spreads = np.apply_along_axis(vfunc,0,probabilities)
bet_vector = np.array(np.where(predicted_spreads > spreads,0,1))
result = np.array(np.where(bet_vector == game_result,1,0))
prob_result = float(np.sum(result)) / len(result)
print prob_result
'''
# for feat in range(1,len(matchups.columns)):
for c in C_vec:
# Create the classifier and check the score
# regress = LogisticRegression()
regress = linear_model.LogisticRegression(C=c,random_state=42)
# selector = RFE(regress)
regress.fit(X_train,y_train)
# Calculate probabilities using the predict_proba method for logistic regression
probabilities = regress.predict_proba(scaler.transform(matchups))
print probabilities
raw_input()
# Vectorize the spread_conversion function and apply the function to the probabilities result vector
vfunc = np.vectorize(spread_conversion)
predicted_spreads = np.apply_along_axis(vfunc,0,probabilities[:,0])
# If the actual line for the home team is lower than the predicted line then you would take the away team, otherwise take the home team
bet_vector = np.array(np.where(predicted_spreads > spreads,0,1))
# Check to see where the bet_vector equals the actual game result with the spread included
result = np.array(np.where(bet_vector == game_result,1,0))
prob_result = float(np.sum(result)) / len(result)
print 'C =',c,' Percent correct =',prob_result#'Number of features =', feat,
if prob_result > prob_val:
prob_val = prob_result
C_val = c
# feat_val = feat
regress = linear_model.LogisticRegression(C=C_val,random_state=42)
regress.fit(X_train,y_train)
filename = ('NFL Regressor.pickle')
pickle_out = open(filename,'wb')
pickle.dump(regress,pickle_out)
pickle_out.close()
print 'Score =',regress.score(X_test,y_test)
print prob_val, C_val#, feat
'''