-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms_seth.py
281 lines (219 loc) · 10.4 KB
/
algorithms_seth.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
import pandas as pd
import os, sys
import pickle,numpy
import matplotlib.pyplot as plt
from sklearn.model_selection import StratifiedKFold
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate, GridSearchCV
from xgboost import XGBClassifier, plot_importance
from sklearn.metrics import log_loss
def load_data(file_name):
# read csv into panda dataframe using headers and found delimiter
tourney_data = pd.read_csv(file_name, delimiter=',')
all_vals = ['Year','T1 TeamID','T1 Seed','T1 SoS','T1 OE','T1 AdjOE','T1 DE','T1 AdjDE','T1 WinDev','T1 AdjWin','T2 TeamID','T2 Seed','T2 SoS','T2 OE','T2 AdjOE','T2 DE','T2 AdjDE','T2 WinDev','T2 AdjWin','Winner']
select_vals = ['T1 Seed','T1 SoS','T1 OE','T1 AdjOE','T1 DE','T1 AdjDE','T1 AdjWin','T2 Seed','T2 SoS','T2 OE','T2 AdjOE','T2 DE','T2 AdjDE','T2 AdjWin','Winner']
tourney_data = tourney_data.loc[:, select_vals]
for row in tourney_data.iterrows():
if row[1].loc['T1 Seed'] > row[1].loc['T2 Seed']:
print("Yes")
return tourney_data.iloc[:,:-1], tourney_data.iloc[:,-1]
def trainXGBoost(data, classes, k_fold, scoring):
params = {
"learning_rate" : [0.01, 0.10, 0.30 ] ,
"max_depth" : [3, 7, 10, 15],
"random_state": [0, 3, 7, 42, 145],
"min_child_weight" : [ 1, 3, 7 ],
"gamma" : [ 0.0, 0.1, 0.2 , 0.4 ],
"colsample_bytree" : [ 0.3, 0.4 , 0.5, 0.7 ],
'subsample' : [ 0.7, 0.9, 1 ],
"booster" : ['gbtree', 'gblinear', 'dart']
}
gbc = XGBClassifier(n_jobs=8,random_state=7, max_depth=3, booster='gbtree', learning_rate=0.1, colsample_bytree=0.3, gamma=0.2, min_child_weight=1, subsample=0.7)
# {'booster': 'gbtree', 'colsample_bytree': 0.3, 'gamma': 0.2, 'learning_rate': 0.1, 'max_depth': 3, 'min_child_weight': 1, 'random_state': 7, 'subsample': 0.7}
glf = GridSearchCV(gbc, params,cv=k_fold, scoring=scoring,refit='loss', return_train_score=True)
glf.fit(data,classes)
print(glf.best_params_)
results = glf.cv_results_
with open('archive/xgb_results.pkl', 'wb') as fp:
pickle.dump(results,fp)
plt.figure(figsize=(13, 13))
plt.title("GridSearchCV Evalution for Loss: XGBoost",
fontsize=16)
plt.xlabel('Evaluation Number')
plt.ylabel("Loss")
ax.set_ylim(0.45, 1)
ax = plt.gca()
# Get the regular numpy array from the MaskedArray
X_axis = numpy.array(range(len(results['param_'+params.keys()[0]].data)), dtype=float)
scorer='loss'
color='g'
sample = 'test'
style = '-'
sample_score_mean = results['mean_%s_%s' % (sample, scorer)]
sample_score_std = results['std_%s_%s' % (sample, scorer)]
sample_score_mean *= -1
sample_score_std *= -1
ax.fill_between(X_axis, sample_score_mean - sample_score_std,
sample_score_mean + sample_score_std,
alpha=0.1 if sample == 'test' else 0, color=color)
ax.plot(X_axis, sample_score_mean, style, color=color,
alpha=1 if sample == 'test' else 0.7,
label="%s (%s)" % (scorer, sample))
best_index = numpy.nonzero(results['rank_test_%s' % scorer] == 1)[0][0]
best_score = results['mean_test_%s' % scorer][best_index]
# Plot a dotted vertical line at the best score for that scorer marked by x
ax.plot([X_axis[best_index], ] * 2, [0, best_score],
linestyle='-.', color=color, marker='x', markeredgewidth=3, ms=8)
# Annotate the best score for that scorer
ax.annotate("%0.2f" % best_score,
(X_axis[best_index], best_score + 0.005))
plt.legend(loc="best")
plt.grid('off')
plt.savefig('figures/xg_param_search_loss.png')
plt.figure(figsize=(13, 13))
plt.title("GridSearchCV Evalution for Accuracy: XGBoost" ,
fontsize=16)
plt.xlabel('Evaluation Number')
plt.ylabel("Accuracy")
ax.set_ylim(0.45, 1)
ax = plt.gca()
# Get the regular numpy array from the MaskedArray
X_axis = numpy.array(range(len(results['param_'+params.keys()[0]].data)), dtype=float)
scorer='accuracy'
color='g'
sample = 'test'
style = '-'
sample_score_mean = results['mean_%s_%s' % (sample, scorer)]
sample_score_std = results['std_%s_%s' % (sample, scorer)]
ax.fill_between(X_axis, sample_score_mean - sample_score_std,
sample_score_mean + sample_score_std,
alpha=0.1 if sample == 'test' else 0, color=color)
ax.plot(X_axis, sample_score_mean, style, color=color,
alpha=1 if sample == 'test' else 0.7,
label="%s (%s)" % (scorer, sample))
best_index = numpy.nonzero(results['rank_test_%s' % scorer] == 1)[0][0]
best_score = results['mean_test_%s' % scorer][best_index]
# Plot a dotted vertical line at the best score for that scorer marked by x
ax.plot([X_axis[best_index], ] * 2, [0, best_score],
linestyle='-.', color=color, marker='x', markeredgewidth=3, ms=8)
# Annotate the best score for that scorer
ax.annotate("%0.2f" % best_score,
(X_axis[best_index], best_score + 0.005))
plt.legend(loc="best")
plt.grid('off')
plt.savefig('figures/xg_param_search_accuracy.png')
gbc_scores = cross_validate(gbc, data, classes, cv=k_fold, scoring=scoring, return_train_score=False, return_estimator=True)
for i in range(len(gbc_scores['estimator'])):
plot_importance(gbc_scores['estimator'][i], importance_type='cover')
plt.savefig('figures/xg_'+str(i)+'_features.png')
return gbc_scores
def trainLogisticRegression(data, classes, k_fold, scoring):
lrc = LogisticRegression(tol=0.001, C=1000, max_iter=100,solver='lbfgs')
# {'C': 1000, 'max_iter': 100, 'penalty': 'l2', 'random_state': 0, 'solver': 'liblinear', 'tol': 0.001}
params = {
'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000],
'tol': [0.001, 0.0001, 0.00001, 0.000001, 0.0000001],
'solver': ['liblinear', 'saga'],
'max_iter': [100,500,1000],
'penalty': ['l1', 'l2'],
"random_state": [0, 1, 3, 7, 9, 10, 42]
}
clf = GridSearchCV(lrc, params,cv=k_fold,scoring=scoring,refit='loss', return_train_score=True)
clf.fit(data,classes)
print(clf.best_params_)
results = clf.cv_results_
with open('archive/lr_results.pkl', 'wb') as fp:
pickle.dump(results,fp)
# for key in params.keys():
plt.figure(figsize=(13, 13))
plt.title("GridSearchCV Evalution for Loss: Logistic Regression" ,
fontsize=16)
plt.xlabel('Evaluation Number')
plt.ylabel("Loss")
ax.set_ylim(0.45, 1)
ax = plt.gca()
# Get the regular numpy array from the MaskedArray
X_axis = numpy.array(range(len(results['param_'+params.keys()[0]].data)), dtype=float)
scorer='loss'
color='g'
sample = 'test'
style = '-'
sample_score_mean = results['mean_%s_%s' % (sample, scorer)]
sample_score_std = results['std_%s_%s' % (sample, scorer)]
sample_score_mean *= -1
sample_score_std *= -1
ax.fill_between(X_axis, sample_score_mean - sample_score_std,
sample_score_mean + sample_score_std,
alpha=0.1 if sample == 'test' else 0, color=color)
ax.plot(X_axis, sample_score_mean, style, color=color,
alpha=1 if sample == 'test' else 0.7,
label="%s (%s)" % (scorer, sample))
best_index = numpy.nonzero(results['rank_test_%s' % scorer] == 1)[0][0]
best_score = results['mean_test_%s' % scorer][best_index]
# Plot a dotted vertical line at the best score for that scorer marked by x
ax.plot([X_axis[best_index], ] * 2, [0, best_score],
linestyle='-.', color=color, marker='x', markeredgewidth=3, ms=8)
# Annotate the best score for that scorer
ax.annotate("%0.2f" % best_score,
(X_axis[best_index], best_score + 0.005))
plt.legend(loc="best")
plt.grid('off')
plt.savefig('figures/lr_param_search_loss.png')
plt.figure(figsize=(13, 13))
plt.title("GridSearchCV Evalution for Accuracy: Logistic Regression" ,
fontsize=16)
plt.xlabel('Evaluation Number')
plt.ylabel("Accuracy")
ax.set_ylim(0.45, 1)
ax = plt.gca()
# Get the regular numpy array from the MaskedArray
X_axis = numpy.array(range(len(results['param_tol'].data)), dtype=float)
scorer='accuracy'
color='g'
sample = 'test'
style = '-'
sample_score_mean = results['mean_%s_%s' % (sample, scorer)]
sample_score_std = results['std_%s_%s' % (sample, scorer)]
ax.fill_between(X_axis, sample_score_mean - sample_score_std,
sample_score_mean + sample_score_std,
alpha=0.1 if sample == 'test' else 0, color=color)
ax.plot(X_axis, sample_score_mean, style, color=color,
alpha=1 if sample == 'test' else 0.7,
label="%s (%s)" % (scorer, sample))
best_index = numpy.nonzero(results['rank_test_%s' % scorer] == 1)[0][0]
best_score = results['mean_test_%s' % scorer][best_index]
# Plot a dotted vertical line at the best score for that scorer marked by x
ax.plot([X_axis[best_index], ] * 2, [0, best_score],
linestyle='-.', color=color, marker='x', markeredgewidth=3, ms=8)
# Annotate the best score for that scorer
ax.annotate("%0.2f" % best_score,
(X_axis[best_index], best_score + 0.005))
plt.legend(loc="best")
plt.grid('off')
plt.savefig('figures/lr_param_search_accuracy.png')
lrc_scores = cross_validate(lrc, data, classes, cv=k_fold, scoring=scoring, return_train_score=False, return_estimator=True)
print(lrc_scores)
return lrc_scores
def trainNaiveBayes(data, classes, k_fold, scoring):
nbc = GaussianNB()
nbc_scores = cross_validate(nbc, data, classes, cv=k_fold, scoring=scoring, return_train_score=False, return_estimator=True)
return nbc_scores
if __name__=="__main__":
file_name = "/".join(__file__.split("/")[:-1])
print(file_name)
if file_name == "":
file_name = "data/tourney_matchup_results_wmirrors.csv"
else:
file_name += "/data/tourney_matchup_results_wmirrors.csv"
scoring = {
'accuracy': 'accuracy',
'loss': 'neg_log_loss',
'roc_auc': 'roc_auc'
}
plt.rcParams.update({'font.size': 16})
data,classes = load_data(file_name)
k_fold = StratifiedKFold(16)
lr = trainLogisticRegression(data, classes, k_fold, scoring)
xg = trainXGBoost(data, classes, k_fold, scoring)
nb = trainNaiveBayes(data, classes, k_fold, scoring)