-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuning.py
25 lines (21 loc) · 1.18 KB
/
tuning.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
import numpy as np
from sklearn.model_selection import GridSearchCV, PredefinedSplit
from sklearn.utils import compute_sample_weight
from sklearn.metrics import make_scorer
def hyperparameter_tuning(estimator, params, normal_data_train, x_val, y_val, verbose=0, scoring='roc_auc'):
features = np.vstack([normal_data_train, x_val])
val_fold = np.concatenate([[-1]*normal_data_train.shape[0], [0]*len(x_val)])
y_true = np.concatenate([[1]*normal_data_train.shape[0], y_val])
ps = PredefinedSplit(test_fold=val_fold)
model = GridSearchCV(estimator, params, scoring=scoring, refit=False, cv=ps, verbose=verbose, n_jobs=-1)
model.fit(features, y_true)
return model.best_params_
def multiclass_tuning(estimator, params, classes, data_train, data_val, verbose=0, scoring='roc_auc'):
best_params = {}
for c in classes:
y_val_c = data_val[1] == c
normal_data_train = data_train[0][data_train[1] == c]
best_params[c] = hyperparameter_tuning(estimator, params, normal_data_train, data_val[0], y_val_c, verbose=verbose, scoring=scoring)
if verbose:
print('Class id ',c ,': best parameters: ', best_params[c], sep='')
return best_params