-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_gridsearch.py
365 lines (294 loc) · 14.3 KB
/
class_gridsearch.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
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
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import time
from sklearn.model_selection import KFold
from sklearn.metrics import *
from sklearn.model_selection import RandomizedSearchCV
import catboost as cat
import warnings
warnings.filterwarnings("ignore")
from tensorflow.keras import backend as K
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
import gc
from hyperopt import hp, fmin, tpe, Trials
from utils import compute_dict_class_weight
def optimization_gridsearch(x, y, model, distributions, time_limit_per_model, nfolds, scoring):
""" gridsearch function (RandomizedSearchCV) for model (not_Neural_Network) with time integrated
Args:
x, y (array or dataframe)
model : apply gridsearch on this model
distribution (dict) : parameters to try
time_limit_per_model (int) : time in seconds to try parameters
nfolds (int) : number of folds during gridsearch
scoring (str) : metric optimized during gridsearch
"""
print_details = False
n_iter = 1
start, last_call = time.perf_counter(), time.perf_counter()
df_results = pd.DataFrame()
df_all_results = pd.DataFrame()
best_n_iter = 0
while time.perf_counter() - start < time_limit_per_model:
clf = RandomizedSearchCV(model, distributions, random_state=None, n_iter=n_iter, cv=nfolds, scoring=scoring)
search = clf.fit(x, y)
approx_time = (time.perf_counter() - last_call) / n_iter
if print_details:
print('time :', approx_time * n_iter, 'and n_iter :', n_iter)
print(search.best_params_)
print(search.best_score_)
print()
if n_iter > best_n_iter:
best_approx_time = approx_time
best_n_iter = n_iter
n_iter = int((time_limit_per_model - (time.perf_counter() - start)) / best_approx_time / 3)
last_call = time.perf_counter()
df_results = pd.concat([df_results, pd.DataFrame(clf.cv_results_)], axis=0).reset_index(drop=True)
if n_iter < 3 and time.perf_counter() - start > time_limit_per_model * 0.8:
break
if n_iter < 1:
break
print(' Total time :', np.round(time.perf_counter() - start, 3), 'and n_iter :', len(df_results))
df_all_results = pd.concat([df_all_results, df_results])
best_mean = np.mean(df_results.mean_test_score)
best_score = np.max(df_results.mean_test_score)
if print_details:
plt.hist(df_results.mean_test_score)
plt.show()
return df_all_results
class GridSearch:
""" Apply gridsearch for sklearn model, catboost, xgboost or lightgbm"""
def __init__(self, model, hyper_params):
"""
Args:
model : apply gridsearch on this model
hyper_params (dict) : parameters to try
"""
self.model = model
self.model_2 = model
self.hyper_params = hyper_params
def train(self, x, y, nfolds=5, scoring='accuracy', verbose=0, time_limit_per_model=60):
self.df_all_results = optimization_gridsearch(x, y, self.model, self.hyper_params, time_limit_per_model, nfolds,
scoring)
self.index_best_score = self.df_all_results.mean_test_score.argmax()
def show_distribution_score(self):
plt.hist(self.df_all_results.mean_test_score)
plt.show()
def best_params(self, print_result=False):
"""
Return:
params (dict) : best parameters from gridsearch
"""
params = self.df_all_results.loc[self.index_best_score, 'params']
print_params = params.copy()
if print_result:
if 'vect__text__tfidf__stop_words' in params.keys() and params['vect__text__tfidf__stop_words'] is not None:
print_params['vect__text__tfidf__stop_words'] = True
if 'vect__tfidf__stop_words' in params.keys() and params['vect__tfidf__stop_words'] is not None:
print_params['vect__tfidf__stop_words'] = True
print('Best parameters: ', print_params)
return params
def best_score(self, print_result=False):
"""
Return:
score (int) : best score from gridsearch
"""
score = self.df_all_results.loc[self.index_best_score, 'mean_test_score']
if print_result:
print('Mean cross-validated score of the best_estimator: ', np.round(score, 4))
return score
def best_estimator(self, objective):
"""
Return:
model : best model from gridsearch
"""
if 'catboost' in str(type(self.model_2)):
return cat.CatBoostClassifier(
random_state=self.model_2.get_param('random_state'),
class_weights=self.model_2.get_param('class_weights'),
verbose=False,
bootstrap_type='Bernoulli',
**self.best_params()
)
else:
return self.model_2.set_params(**self.best_params())
def get_grid(self, sort_by='mean_test_score'):
return self.df_all_results[['mean_fit_time', 'params', 'mean_test_score', 'std_test_score']].sort_values(
by=sort_by, ascending=False).reset_index(drop=True)
##############################
##############################
##############################
class GridSearch_NN:
""" Apply gridsearch for Neural Network model """
def __init__(self, Model_NN, hyper_params):
self.Model_NN = Model_NN
self.hyper_params = hyper_params
def optimise(self, params):
""" function to optimize by hyperopt library
Args :
params (dict) : parameters to try
"""
self.Model_NN.initialize_params(self.x, self.y, params)
print(self.Model_NN.p)
oof_val = np.zeros((self.y.shape[0], self.y.shape[1]))
start = time.perf_counter()
for n, (tr, te) in enumerate(KFold(n_splits=self.nfolds,
random_state=self.Model_NN.seed,
shuffle=True).split(self.y)):
if isinstance(self.x, dict):
x_tr, x_val = {}, {}
for col in self.x.keys():
x_tr[col], x_val[col] = self.x[col][tr], self.x[col][te]
y_tr, y_val = self.y.values[tr], self.y.values[te]
elif isinstance(self.x, list):
x_tr, x_val = [], []
for col in range(len(self.x)):
x_tr.append(self.x[col][tr])
x_val.append(self.x[col][te])
y_tr, y_val = self.y.values[tr], self.y.values[te]
else:
x_tr, x_val = self.x.values[tr], self.x.values[te]
y_tr, y_val = self.y.values[tr], self.y.values[te]
model = self.Model_NN.model()
if self.scoring == 'accuracy':
monitor = 'accuracy'
else:
monitor = 'loss'
rlr = ReduceLROnPlateau(monitor='val_' + monitor, factor=0.1, patience=3,
verbose=0, min_delta=1e-4, mode='auto', min_lr=1e-4)
# ckp = ModelCheckpoint(f'model_{n}.hdf5', monitor = 'val_loss', verbose = 0,
# save_best_only = True, save_weights_only = True, mode = 'min')
es = EarlyStopping(monitor='val_' + monitor, min_delta=0.0001, patience=4, mode='auto',
baseline=None, restore_best_weights=True, verbose=0)
history = model.fit(x_tr, y_tr, validation_data=(x_val, y_val),
epochs=60, batch_size=16,
class_weight=compute_dict_class_weight(y_tr, self.Model_NN.class_weight,
self.Model_NN.objective),
callbacks=[rlr, es], verbose=0)
hist = pd.DataFrame(history.history)
if 'binary_proba' in self.Model_NN.objective:
oof_val[te, :] = model.predict(x_val)
else:
oof_val[te, :] = np.argmax(model.predict(x_val), axis=1).reshape(-1, 1)
self.total_epochs += len(history.history['val_loss'][:-5])
K.clear_session()
del model, history, hist
d = gc.collect()
metrics = []
oof_val = np.where(oof_val > 0.5, 1, 0).reshape(-1)
if 'f1' in self.scoring:
metrics.append(-f1_score(self.y.values.reshape(-1), oof_val))
elif 'recall' in self.scoring:
metrics.append(-recall_score(self.y.values.reshape(-1), oof_val))
elif 'precision' in self.scoring:
metrics.append(-precision_score(self.y.values.reshape(-1), oof_val))
elif 'roc' in self.scoring or 'auc' in self.scoring:
metrics.append(-roc_auc_score(self.y.values.reshape(-1), oof_val))
else:
metrics.append(-accuracy_score(self.y.values.reshape(-1), oof_val))
score = -np.mean(metrics)
print('oof_val score', self.scoring, 'Metric', score)
if 'hidden_units' in self.Model_NN.p.keys():
self.list_hist[len(self.Model_NN.p['hidden_units']) - 1].append(score)
else:
self.list_hist[0].append(score)
self.df_all_results['mean_fit_time'].append(time.perf_counter() - start)
self.df_all_results['params'].append(params)
self.df_all_results['mean_test_score'].append(score)
self.df_all_results['std_test_score'].append(0) # just 0
return np.mean(metrics)
def train(self, x_, y_, nfolds=5, scoring='accuracy', verbose=0, time_limit_per_model=60,
name_model='SimpleNeuralNetwork'):
self.x = x_ # .copy().reset_index(drop=True)
self.y = y_ # .copy().reset_index(drop=True)
self.nfolds = nfolds
self.scoring = scoring
self.df_all_results = {'mean_fit_time': [], 'params': [], 'mean_test_score': [], 'std_test_score': []}
self.list_hist = [[] for name in self.hyper_params.keys() if 'hidden_unit' in name]
if len(self.list_hist) == 0:
self.list_hist = [[]]
self.total_epochs = 0
trials = Trials()
self.hopt = fmin(fn=self.optimise,
space=self.hyper_params,
algo=tpe.suggest,
max_evals=100,
timeout=time_limit_per_model,
trials=trials,
)
self.df_all_results = pd.DataFrame(self.df_all_results)
self.df_all_results['model'] = name_model
self.index_best_score = self.df_all_results.mean_test_score.argmax()
self.mean_epochs = int(self.total_epochs / self.nfolds) + 1
def show_distribution_score(self):
rows, cols = 1, 3
fig, ax = plt.subplots(rows, cols, figsize=(50, 20))
for row in range(rows):
for col in range(cols):
if row * cols + col + 1 <= len(self.list_hist) and len(self.list_hist[row * cols + col]) > 0:
ax[col].hist(self.list_hist[row * cols + col])
for tick in ax[col].xaxis.get_major_ticks():
tick.label.set_fontsize(30)
plt.show()
def best_params(self, print_result=False):
"""
Return:
params (dict) : best parameters from gridsearch
"""
params = self.df_all_results.loc[self.index_best_score, 'params']
if print_result:
print('Best parameters: ', params)
return params
def best_score(self, print_result=False):
"""
Return:
score (int) : best score from gridsearch
"""
score = self.df_all_results.loc[self.index_best_score, 'mean_test_score']
if print_result:
print('Mean cross-validated score of the best_estimator: ', np.round(score, 4))
return score
def best_estimator(self, objective):
""" fit a model with best parameters and only on one fold (add a break)
Return:
model : best model from hyperopt
"""
self.Model_NN.initialize_params(self.x, self.y, self.best_params())
model = self.Model_NN.model()
if self.scoring == 'accuracy':
monitor = 'accuracy'
else:
monitor = 'loss'
rlr = ReduceLROnPlateau(monitor='val_' + monitor, factor=0.1, patience=3,
verbose=1, min_delta=1e-4, mode='auto', min_lr=1e-4)
# ckp = ModelCheckpoint(f'model_{n}.hdf5', monitor = 'val_loss', verbose = 0,
# save_best_only = True, save_weights_only = True, mode = 'min')
es = EarlyStopping(monitor='val_' + monitor, min_delta=0.0001, patience=5, mode='auto',
baseline=None, restore_best_weights=True, verbose=0)
for n, (tr, te) in enumerate(KFold(n_splits=10,
random_state=self.Model_NN.seed,
shuffle=True).split(self.y)):
if isinstance(self.x, dict):
x_tr, x_val = {}, {}
for col in self.x.keys():
x_tr[col], x_val[col] = self.x[col][tr], self.x[col][te]
y_tr, y_val = self.y.values[tr], self.y.values[te]
elif isinstance(self.x, list):
x_tr, x_val = [], []
for col in range(len(self.x)):
x_tr.append(self.x[col][tr])
x_val.append(self.x[col][te])
y_tr, y_val = self.y.values[tr], self.y.values[te]
else:
x_tr, x_val = self.x.values[tr], self.x.values[te]
y_tr, y_val = self.y.values[tr], self.y.values[te]
history = model.fit(x_tr, y_tr, validation_data=(x_val, y_val),
epochs=60, batch_size=16,
class_weight=compute_dict_class_weight(y_tr, self.Model_NN.class_weight,
self.Model_NN.objective),
callbacks=[rlr, es], verbose=0)
break # best_model train on only one validation
return model
def get_grid(self, sort_by='mean_test_score'):
return self.df_all_results[['mean_fit_time', 'params', 'mean_test_score', 'std_test_score']].sort_values(
by=sort_by, ascending=False).reset_index(drop=True)