-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
391 lines (319 loc) · 11.1 KB
/
model.py
File metadata and controls
391 lines (319 loc) · 11.1 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
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score, roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
data_dir = "NFL Play by Play 2009-2018 (v5).csv"
nfl_df = pd.read_csv(data_dir, low_memory=False)
print("COLUMNS IN DATASET")
print(nfl_df.columns.values)
homescores = {}
awayscores = {}
homescores = nfl_df['total_home_score'].groupby(nfl_df['game_id']).max()
awayscores = nfl_df['total_away_score'].groupby(nfl_df['game_id']).max()
nfl_df['final_home'] = nfl_df['game_id'].map(homescores)
nfl_df['final_away'] = nfl_df['game_id'].map(awayscores)
def getScoreDifferential(nfl_df):
homeScore = nfl_df['final_home']
awayScore = nfl_df['final_away']
scoreDifferential = homeScore - awayScore
if scoreDifferential <= 0:
return 0
elif scoreDifferential > 0:
return 1
def determineTeam(nfl_df):
if (nfl_df['posteam'] == nfl_df['home_team']):
return 1
else:
return 0
def isRedZone(nfl_df):
if (nfl_df['yardline_100'] <= 20):
return 1
else:
return 0
def is3Points(nfl_df):
if (nfl_df['field_goal_result'] == "made"):
return 1
else:
return 0
def is2Points(nfl_df):
if(nfl_df['two_point_conv_result'] == "success"):
return 1
else:
return 0
def is1Point(nfl_df):
if(nfl_df['extra_point_result'] == 'good'):
return 1
else:
return 0
def isPenalty(nfl_df):
if (nfl_df['penalty_team'] == nfl_df['home_team']):
return 1
else:
return 0
def isTD(nfl_df):
if (nfl_df['posteam_score_post'] - nfl_df['posteam_score'] == 6):
return 1
elif (nfl_df['defteam_score_post'] - nfl_df['defteam_score'] == 6):
return 1
else:
return 0
def isAFCW(val):
if val == 'KC' or val == 'SD' or val == 'LAC' or val == 'OAK' or val == 'DEN':
return 1
else:
return 0
def isAFCN(val):
if val == 'PIT' or val == 'CLE' or val == 'CIN' or val == 'BAL':
return 1
else:
return 0
def isAFCS(val):
if val == 'JAC' or val == 'JAX' or val == 'HOU' or val == 'IND' or val == 'TEN':
return 1
else:
return 0
def isAFCE(val):
if val == 'MIA' or val == 'NE' or val == 'NYJ' or val == 'BUF':
return 1
else:
return 0
def isNFCN(val):
if val == 'MIN' or val == 'DET' or val == 'CHI' or val == 'GB':
return 1
else:
return 0
def isNFCW(val):
if val == 'SEA' or val == 'LA' or val == 'STL' or val == 'ARI' or val == 'SF':
return 1
else:
return 0
def isNFCS(val):
if val == 'CAR' or val == 'NO' or val == 'TB' or val == 'ATL':
return 1
else:
return 0
def isNFCE(val):
if val == 'NYG' or val == 'WAS' or val == 'DAL' or val == 'PHI':
return 1
else:
return 0
# top active QB win percentages
def isTomBrady(val):
if (val == 'T.Brady'):
return 1
else:
return 0
def isDrewBrees(val):
if (val == 'D.Brees'):
return 1
else:
return 0
def isAaronRodgers(val):
if (val == 'A.Rodgers'):
return 1
else:
return 0
def isBenRoethlisberger(val):
if (val == 'B.Roethlisberger'):
return 1
else:
return 0
def isPeytonManning(val):
if (val == 'P.Manning'):
return 1
else:
return 0
def isRussellWilson(val):
if (val == 'R.Wilson'):
return 1
else:
return 0
def isAndrewLuck(val):
if (val == 'A.Luck'):
return 1
else:
return 0
# lowest active QB win percentages
def isRyanFitzpatrick(val):
if (val == 'R.Fitzpatrick'):
return 1
else:
return 0
def isMatthewStafford(val):
if (val == 'M.Stafford'):
return 1
else:
return 0
# engineered features
nfl_df['Brady'] = nfl_df['passer_player_name'].apply(isTomBrady)
nfl_df['Brees'] = nfl_df['passer_player_name'].apply(isDrewBrees)
nfl_df['Rodgers'] = nfl_df['passer_player_name'].apply(isAaronRodgers)
nfl_df['Roethlisberger'] = nfl_df['passer_player_name'].apply(isBenRoethlisberger)
nfl_df['Manning'] = nfl_df['passer_player_name'].apply(isPeytonManning)
nfl_df['Wilson'] = nfl_df['passer_player_name'].apply(isRussellWilson)
nfl_df['Luck'] = nfl_df['passer_player_name'].apply(isAndrewLuck)
nfl_df['Fitzpatrick'] = nfl_df['passer_player_name'].apply(isRyanFitzpatrick)
nfl_df['Stafford'] = nfl_df['passer_player_name'].apply(isMatthewStafford)
# Engineered Feature to determine if a possession team is also the home team
nfl_df['isHome'] = nfl_df.apply(determineTeam, axis=1)
# Engineered Feature to determine whether a team is inside the red zone
nfl_df['redZone'] = nfl_df.apply(isRedZone, axis=1)
# Engineered Feaure to determine whether a team made a field goal
nfl_df['made3Points'] = nfl_df.apply(is3Points, axis=1)
# Engineered Feaure to determine if a team is AFC NORTH
nfl_df['AFCNorth'] = nfl_df['home_team'].apply(isAFCN)
# Engineered Feaure to determine if a team is AFC
nfl_df['AFCWest'] = nfl_df['home_team'].apply(isAFCW)
# Engineered Feaure to determine if a team is AFC
nfl_df['AFCSouth'] = nfl_df['home_team'].apply(isAFCS)
# Engineered Feaure to determine if a team is AFC
nfl_df['AFCEast'] = nfl_df['home_team'].apply(isAFCE)
# Engineered Feaure to determine if a team is AFC
nfl_df['NFCNorth'] = nfl_df['home_team'].apply(isNFCN)
# Engineered Feaure to determine if a team is AFC
nfl_df['NFCWest'] = nfl_df['home_team'].apply(isNFCW)
# Engineered Feaure to determine if a team is AFC
nfl_df['NFCSouth'] = nfl_df['home_team'].apply(isNFCS)
# Engineered Feaure to determine if a team is AFC
nfl_df['NFCEast'] = nfl_df['home_team'].apply(isNFCE)
#Engineered Feaure to determine if team made 2 point conv
nfl_df['made2points'] = nfl_df.apply(is2Points, axis = 1)
#Engineered Feature to determine if TD occured
nfl_df['is6Pts'] = nfl_df.apply(isTD, axis = 1)
#Engineered Feature for extra point
nfl_df['made1point'] = nfl_df.apply(is1Point, axis = 1)
#Engineered Feautre to determine wheich team got a penalty
nfl_df['Penalty'] = nfl_df.apply(isPenalty, axis = 1)
nfl_df['posteam_score_pre'] = nfl_df['posteam_score'].shift()
nfl_df['defteam_score_pre'] = nfl_df['defteam_score'].shift()
nfl_df['score_differential_pre'] = nfl_df['score_differential'].shift()
nfl_df.drive = nfl_df.drive.astype(float)
replaceFeatures = [
'interception',
'fourth_down_converted',
'fourth_down_failed',
'fumble',
'safety',
'touchdown',
'sack',
'third_down_converted',
'third_down_failed',
'punt_attempt',
'tackled_for_loss',
'punt_blocked',
'punt_attempt',
'isHome',
'no_huddle',
'shotgun',
'made3Points',
'incomplete_pass'
]
elimFeatures = [
'game_seconds_remaining',
'field_goal_result_new',
'two_point_conv_result_new',
'extra_point_result_new',
'ydstogo',
'home_timeouts_remaining',
'away_timeouts_remaining',
'redZone',
'NFCNorth',
'NFCSouth',
'NFCWest',
'NFCEast',
'AFCNorth',
'AFCSouth',
'AFCEast',
'AFCWest',
'Brady',
'Brees',
'Rodgers',
'Roethlisberger',
'Manning',
'Wilson',
'Luck',
'Fitzpatrick',
'Stafford',
'posteam_score',
'defteam_score',
'score_differential',
'yardline_100'
]
# Eliminates any data set with an entire row and column of null values
nfl_df.dropna(axis=1, how='all', inplace = True)
nfl_df.dropna(axis=0, how='all', inplace=True)
# Impute 0 for all the missing NULL values
nfl_df[replaceFeatures] = nfl_df[replaceFeatures].fillna(0)
nfl_df['field_goal_result'] = nfl_df['field_goal_result'].fillna("N/A")
nfl_df['two_point_conv_result'] = nfl_df['two_point_conv_result'].fillna("N/A")
nfl_df['extra_point_result'] = nfl_df['extra_point_result'].fillna("N/A")
nfl_df['posteam_score'] = nfl_df['posteam_score'].fillna(nfl_df['posteam_score_pre'])
nfl_df['defteam_score'] = nfl_df['defteam_score'].fillna(nfl_df['defteam_score_pre'])
nfl_df['score_differential'] = nfl_df['score_differential'].fillna(nfl_df['score_differential_pre'])
nfl_df['score_differential'] = nfl_df['score_differential'].fillna(0)
nfl_df['score_differential_pre'] = nfl_df['score_differential_pre'].fillna(0)
nfl_df['posteam_score_pre'] = nfl_df['posteam_score_pre'].fillna(0)
nfl_df['posteam_score'] = nfl_df['posteam_score'].fillna(0)
nfl_df['defteam_score'] = nfl_df['defteam_score'].fillna(0)
nfl_df['defteam_score_pre'] = nfl_df['defteam_score_pre'].fillna(0)
nfl_df = nfl_df.dropna(axis=0, subset=['yardline_100', 'game_seconds_remaining'])
features = replaceFeatures + elimFeatures
# Label Encoder for field goal result
le = LabelEncoder()
nfl_df['field_goal_result_new'] = le.fit_transform(nfl_df['field_goal_result'])
# Label Encoder for 2 pt conversion
le1 = LabelEncoder()
nfl_df['two_point_conv_result_new'] = le1.fit_transform(nfl_df['two_point_conv_result'])
# Label Encoder for Extra Point
le2 = LabelEncoder()
nfl_df['extra_point_result_new'] = le2.fit_transform(nfl_df['extra_point_result'])
# Evaluating the data types and making sure the features in the array are floats
# print("DATA TYPES OF FEATURES")
# print(nfl_df[features].dtypes)
# Evaluate how many null values we have for each element in the 'features' array
missing_values_count = (nfl_df[features].isnull().sum())
print()
print("NULL VALUES LEFT IN FEATURES")
print(missing_values_count)
print()
print("CLEANED DATAFRAME")
print(nfl_df)
# determine features and y-label
X = nfl_df[features]
nfl_df['win'] = nfl_df.apply(getScoreDifferential, axis=1)
y = nfl_df['win']
# build the model
model = RandomForestClassifier(max_depth=15, n_estimators=125, min_samples_leaf=50)
# Use TRAIN_TEST_SPlIT for large datasets and when large datasets take too long to cross-validate
# Use sklearn to split df into train/test sets
train_X, test_X, train_y, test_y = train_test_split(X, y, train_size=0.8, test_size=0.2)
model.fit(train_X, train_y)
predictions_train = model.predict_proba(train_X)
predictions_test = model.predict_proba(test_X)
# Score the train/test splits
print("ROC AUC")
auc = roc_auc_score(train_y, predictions_train[:, 1])
print("train: " + str(auc))
auc = roc_auc_score(test_y, predictions_test[:, 1])
print("test: " + str(auc))
# Evaluate the feature importances of the training features
featureImportance = model.feature_importances_
featureImportance_df = pd.DataFrame(featureImportance, index=train_X.columns, columns=["rate of importance"])
print(featureImportance_df)
# Use CROSS_VAL_SCORE on smaller datasets or when the process doesn't add signifcant run time
# Use sklearn to run cross validation on (5) folds of the data
cross_validation = cross_val_score(model, X, y, cv=5, scoring='roc_auc')
print("ROC AUC CROSS VALIDATION")
print(cross_validation)
print("ROC AUC MEAN")
print(np.mean(cross_validation))
nfl_df['Probability'] = model.predict_proba(X)[:,1]
nfl_df['group'] = 0
tr_indices = [index for index, values in enumerate (train_y) if values == 1]
nfl_df['group'] = nfl_df.loc[nfl_df.index.isin(tr_indices),'group'] = 1
nfl_df.head()
nfl_df.to_csv('output.csv')
print("Test Results were successfully saved!")