-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_dataset_selection.py
296 lines (250 loc) · 11.3 KB
/
model_dataset_selection.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
289
290
291
292
293
294
295
296
import pickle
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
# from sklearn.model_selection import KFold
from sklearn.model_selection import train_test_split
# from sklearn.model_selection import cross_val_score
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, AdaBoostClassifier
from sklearn.svm import SVC
from filters import games_up_to_2018_season_filter, season2018_filter
# def games_up_to_2018_season_filter(df):
# '''Filter for games up to 2018 season'''
# notourney2018 = (df['GameType'] != 'tourney2018')
# noseason2018 = (df['GameType'] != 'season2018')
# games_up_to_2018_season = df[notourney2018 & noseason2018]
# return games_up_to_2018_season
#
# def season2018_filter(df):
# '''Filter for 2018 season games'''
# season2018cond = (df['GameType'] == 'season2018')
# season2018 = df[season2018cond]
# return season2018
def set_up_data_for_model(df, model_data='cluster'):
'''
Inputs: Model DataFrame and DataFrame Version (gamelogs, experience, cluster)
Outputs: Vectors for model
'''
games_up_to_2018_season = games_up_to_2018_season_filter(df)
season2018 = season2018_filter(df)
if model_data == 'gamelogs':
Xy_train = games_up_to_2018_season[['W', 'Wp', 'ppg', 'pApg', 'FGp',
'3Pp', 'FTp', 'ORBpg', 'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg',
'PFpg', 'sos', 'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp', 'OPFTp',
'OPORBpg', 'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg', 'OPTOpg',
'OPPFpg', 'OPsos']]
Xy_test = season2018[['W', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp',
'ORBpg', 'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp', 'OPFTp', 'OPORBpg',
'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg', 'OPTOpg', 'OPPFpg',
'OPsos']]
elif model_data == 'experience':
Xy_train = games_up_to_2018_season[['W', 'Wp', 'ppg', 'pApg', 'FGp',
'3Pp', 'FTp', 'ORBpg', 'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg',
'PFpg', 'sos', 'exp_factor', 'OPWp', 'OPppg', 'OPpApg', 'OPFGp',
'OP3Pp', 'OPFTp', 'OPORBpg', 'OPRBpg', 'OPASTpg', 'OPSTLpg',
'OPBLKpg', 'OPTOpg', 'OPPFpg', 'OPsos', 'OPexp_factor']]
Xy_test = season2018[['W', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp',
'ORBpg', 'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'exp_factor', 'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp', 'OPFTp',
'OPORBpg', 'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg', 'OPTOpg',
'OPPFpg', 'OPsos', 'OPexp_factor']]
elif model_data == 'cluster':
Xy_train = games_up_to_2018_season[['W', 'Wp', 'ppg', 'pApg', 'FGp',
'3Pp', 'FTp', 'ORBpg', 'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg',
'PFpg', 'sos', 'exp_factor', 'C0', 'C1', 'C2', 'F0', 'F1', 'F2',
'G0', 'G1', 'G2', 'G3', 'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp',
'OPFTp', 'OPORBpg', 'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg',
'OPTOpg', 'OPPFpg', 'OPsos', 'OPexp_factor', 'C0', 'C1', 'C2',
'F0', 'F1', 'F2', 'G0', 'G1', 'G2', 'G3']]
Xy_test = season2018[['W', 'Wp', 'ppg', 'pApg', 'FGp', '3Pp', 'FTp',
'ORBpg', 'RBpg', 'ASTpg', 'STLpg', 'BLKpg', 'TOpg', 'PFpg', 'sos',
'exp_factor', 'C0', 'C1', 'C2', 'F0', 'F1', 'F2', 'G0', 'G1', 'G2',
'G3', 'OPWp', 'OPppg', 'OPpApg', 'OPFGp', 'OP3Pp', 'OPFTp',
'OPORBpg', 'OPRBpg', 'OPASTpg', 'OPSTLpg', 'OPBLKpg', 'OPTOpg',
'OPPFpg', 'OPsos', 'OPexp_factor', 'C0', 'C1', 'C2', 'F0', 'F1',
'F2', 'G0', 'G1', 'G2', 'G3']]
# Set up features and targets
X_train = Xy_train.iloc[:, 1:].as_matrix()
y_train = Xy_train.iloc[:, 0].as_matrix()
X_test = Xy_test.iloc[:, 1:].as_matrix()
y_test = Xy_test.iloc[:, 0].as_matrix()
return X_train, y_train, X_test, y_test
def pipe_model(model, X_train, y_train, X_test, y_test):
'''
Set up logistic regession pipeline.
Input: train and test matricies
Output: model predictions and accuracy
'''
pipeline = Pipeline(steps=[('Standard Scaler', StandardScaler()),
('Model', model())
])
pipeline.fit(X_train, y_train)
predicitons = pipeline.predict(X_test)
score = metrics.accuracy_score(y_test, predicitons)
return score #, predicitons
def lr_pipe_model(X_train, y_train, X_test, y_test):
'''
Set up logistic regession pipeline.
Input: train and test matricies
Output: model predictions and accuracy
'''
pipeline = Pipeline(steps=[('Standard Scaler', StandardScaler()),
('Logistic Regression', LogisticRegression())
])
pipeline.fit(X_train, y_train)
predicitons = pipeline.predict(X_test)
score = metrics.accuracy_score(y_test, predicitons)
return score #, predicitons
def rf_pipe_model(X_train, y_train, X_test, y_test):
'''
Set up RandomForest Classification pipeline.
Input: train and test matricies
Output: model predictions and accuracy
'''
pipeline = Pipeline(steps=[('Standard Scaler', StandardScaler()),
('RandomForest Classification', RandomForestClassifier(n_estimators=200))
])
pipeline.fit(X_train, y_train)
predicitons = pipeline.predict(X_test)
score = metrics.accuracy_score(y_test, predicitons)
return score #, predicitons
# GradientBoostingClassifier, AdaBoostClassifier, SVC
def gdb_pipe_model(X_train, y_train, X_test, y_test):
'''
Set up RandomForest Classification pipeline.
Input: train and test matricies
Output: model predictions and accuracy
'''
pipeline = Pipeline(steps=[('Standard Scaler', StandardScaler()),
('GradientBoosting Classification', GradientBoostingClassifier())
])
pipeline.fit(X_train, y_train)
predicitons = pipeline.predict(X_test)
score = metrics.accuracy_score(y_test, predicitons)
return score #, predicitons
def adaboost_pipe_model(X_train, y_train, X_test, y_test):
'''
Set up RandomForest Classification pipeline.
Input: train and test matricies
Output: model predictions and accuracy
'''
pipeline = Pipeline(steps=[('Standard Scaler', StandardScaler()),
('AdaBoost Classification', AdaBoostClassifier())
])
pipeline.fit(X_train, y_train)
predicitons = pipeline.predict(X_test)
score = metrics.accuracy_score(y_test, predicitons)
return score #, predicitons
def svc_pipe_model(X_train, y_train, X_test, y_test):
'''
Set up RandomForest Classification pipeline.
Input: train and test matricies
Output: model predictions and accuracy
'''
pipeline = Pipeline(steps=[('Standard Scaler', StandardScaler()),
('SVC Classification', AdaBoostClassifier())
])
pipeline.fit(X_train, y_train)
predicitons = pipeline.predict(X_test)
score = metrics.accuracy_score(y_test, predicitons)
return score #, predicitons
def test_datasets(list_of_datasets, pipeline, dataset_type='gamelogs'):
accuracies = []
i = 0
for dataset in list_of_datasets:
X_train, y_train, X_test, y_test = set_up_data_for_model(dataset, dataset_type)
score = pipeline(X_train, y_train, X_test, y_test)
accuracies.append(score)
rolling_ave = np.arange(2, 8)
print('{} game rolling ave Accuracy = {:.4f}'.format(rolling_ave[i], score))
i += 1
return accuracies
def accuracy_bars(x, y):
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111)
ax.bar(rolling_ave, accuracies, width=0.4, color='blue')
ax.set_ylim(0.6,0.7)
ax.set_ylabel('Accuracy')
ax.set_title('Accuracy Scores by rolling average {} Dataset'.format(dataset_type))
xTickMarks = [str(n)+'G_Roll' for n in range(1,8)]
xtickNames = ax.set_xticklabels(xTickMarks)
plt.setp(xtickNames, rotation=45, fontsize=10)
plt.show()
if __name__ == '__main__':
'''
Read in model data.
'''
games_roll2 = pd.read_pickle('2_model_data/gamelog_2_exp_clust.pkl')
games_roll3 = pd.read_pickle('2_model_data/gamelog_3_exp_clust.pkl')
games_roll4 = pd.read_pickle('2_model_data/gamelog_4_exp_clust.pkl')
games_roll5 = pd.read_pickle('2_model_data/gamelog_5_exp_clust.pkl')
games_roll6 = pd.read_pickle('2_model_data/gamelog_6_exp_clust.pkl')
games_roll7 = pd.read_pickle('2_model_data/gamelog_7_exp_clust.pkl')
'''
Test datasets with various models.
'''
datasets = [games_roll2, games_roll3, games_roll4,
games_roll5, games_roll6, games_roll7]
print('Logistic Regression')
print('\n')
print('Gamelogs')
test_datasets(datasets, lr_pipe_model, dataset_type='gamelogs')
print('\n')
print('Gamelogs with experience factor')
test_datasets(datasets, lr_pipe_model, dataset_type='experience')
print('\n')
print('Gamelogs with experience factor and team compostition clusters')
test_datasets(datasets, lr_pipe_model, dataset_type='cluster')
print('\n')
print('\n')
print('Random Forest')
print('\n')
# print('Gamelogs')
# test_datasets(datasets, rf_pipe_model, dataset_type='gamelogs')
# print('\n')
# print('Gamelogs with experience factor')
# test_datasets(datasets, rf_pipe_model, dataset_type='experience')
# print('\n')
print('Gamelogs with experience factor and team compostition clusters')
test_datasets(datasets, rf_pipe_model, dataset_type='cluster')
# print('\n')
# print('\n')
# print('Gradient Boosting')
# print('\n')
# print('Gamelogs')
# test_datasets(datasets, gdb_pipe_model, dataset_type='gamelogs')
# print('\n')
# print('Gamelogs with experience factor')
# test_datasets(datasets, gdb_pipe_model, dataset_type='experience')
# print('\n')
# print('Gamelogs with experience factor and team compostition clusters')
# test_datasets(datasets, gdb_pipe_model, dataset_type='cluster')
# print('\n')
# print('\n')
# print('AdaBoost')
# print('\n')
# print('Gamelogs')
# test_datasets(datasets, adaboost_pipe_model, dataset_type='gamelogs')
# print('\n')
# print('Gamelogs with experience factor')
# test_datasets(datasets, adaboost_pipe_model, dataset_type='experience')
# print('\n')
# print('Gamelogs with experience factor and team compostition clusters')
# test_datasets(datasets, adaboost_pipe_model, dataset_type='cluster')
# print('\n')
# print('\n')
# print('Support Vector Classifier')
# print('\n')
# print('Gamelogs')
# test_datasets(datasets, svc_pipe_model, dataset_type='gamelogs')
# print('\n')
# print('Gamelogs with experience factor')
# test_datasets(datasets, svc_pipe_model, dataset_type='experience')
# print('\n')
# print('Gamelogs with experience factor and team compostition clusters')
# test_datasets(datasets, svc_pipe_model, dataset_type='cluster')