-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
129 lines (105 loc) · 5.24 KB
/
models.py
File metadata and controls
129 lines (105 loc) · 5.24 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
import xgboost as xgb
from sklearn.model_selection import GridSearchCV
from sklearn import svm
from sklearn.metrics import f1_score, roc_auc_score
import time
from hyperopt import STATUS_OK, Trials, fmin, tpe
from sklearn.model_selection import GridSearchCV, StratifiedKFold, cross_val_score
import numpy as np
def tune_svm(X, y, param_grid: dict, isMulticlass: bool = False, cv: int = 2) -> svm.SVC:
"""Tune SVM model hyperparameters using GridSearchCV and sklearn API.
Args:
X (any): input data
y (any): data labels
param_grid (dict): dictionary of parameters to tune for SVM using GridSearchCV
cv (int, optional): number of cross-validation folds. Defaults to 2.
Returns:
best_model: best performing SVM classifier model from GridSearchCV
"""
#create SVM model for tuning
svm_model = svm.SVC()
if isMulticlass:
grid = GridSearchCV(svm_model, scoring='f1_macro',param_grid=param_grid, cv=cv, verbose=1, return_train_score=True)
else:
grid = GridSearchCV(svm_model, scoring='roc_auc', param_grid=param_grid, cv=cv, verbose=1, return_train_score=True)
grid.fit(X, y)
# Print the best parameters and score
print(f"Best parameters: {str(grid.best_params_)}")
print(f"Best score: {str(grid.best_score_)}")
return grid.best_estimator_
def hyperopt_multiclass(param_space: any, xTrain: any, yTrain: any, xTest: any, yTest: any, num_rounds: int = 100):
"""Hyperparameter tuning using Bayesian Optimization for a multiclass XGBoost classifier using hyperopt library.
Args:
param_space (any): hyperparameter space to tune
xTrain (any): training data
yTrain (any): training labels
xTest (any): test data
yTest (any): test labels
num_rounds (int, optional): maximum number of parameter combination rounds. Defaults to 100.
Returns:
xgb.XGBClassifier, dict: best performing XGBoost classifier model and best parameters
"""
start = time.time()
def objective(param_space):
clf = xgb.XGBClassifier(**param_space)
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
score = cross_val_score(clf, xTrain, yTrain, cv=skf, scoring='f1_macro').mean()
return {'loss': -score, 'status': STATUS_OK}
trials = Trials()
best_param = fmin(fn=objective,
space=param_space,
algo=tpe.suggest,
max_evals=num_rounds,
trials=trials)
loss = [x['result']['loss'] for x in trials.trials]
#ensure best_param dictionary has right data types
best_param['max_depth'] = int(best_param['max_depth'])
best_param['tree_method'] = ['exact', 'gpu_hist'][best_param['tree_method']]
best_param['objective'] = ['multi:softmax', 'multi:softprob'][best_param['objective']]
best_clf = xgb.XGBClassifier(**best_param)
best_clf.fit(xTrain, yTrain)
print('==============RESULTS==============')
print('Best parameters: ', best_param)
print('Best loss: ', -np.min(loss))
print('Time taken: ', time.time() - start)
print('Test accuracy: ', f1_score(yTest, best_clf.predict(xTest), average='macro'))
print('Parameter combinations evaluated: ', len(trials.trials))
return best_clf, best_param
def hyperopt_binary(param_space: any, xTrain: any, yTrain: any, xTest: any, yTest: any, num_rounds: int = 100):
"""Hyperparameter tuning using Bayesian Optimization for a binary XGBoost classifier using hyperopt library.
Args:
param_space (any): hyperparameter space to tune
xTrain (any): training data
yTrain (any): training labels
xTest (any): test data
yTest (any): test labels
num_rounds (int, optional): maximum number of parameter combination rounds. Defaults to 100.
Returns:
xgb.XGBClassifier, dict: best performing XGBoost classifier model and best parameters
"""
start = time.time()
def objective(param_space):
clf = xgb.XGBClassifier(**param_space)
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
score = cross_val_score(clf, xTrain, yTrain, cv=skf, scoring='roc_auc').mean()
return {'loss': -score, 'status': STATUS_OK}
trials = Trials()
best_param = fmin(fn=objective,
space=param_space,
algo=tpe.suggest,
max_evals=num_rounds,
trials=trials)
loss = [x['result']['loss'] for x in trials.trials]
#ensure best_param dictionary has right data types
best_param['max_depth'] = int(best_param['max_depth'])
best_param['tree_method'] = ['exact', 'gpu_hist'][best_param['tree_method']]
best_param['objective'] = ['binary:logistic', 'binary:logitraw', 'binary:hinge'][best_param['objective']]
best_clf = xgb.XGBClassifier(**best_param)
best_clf.fit(xTrain, yTrain)
print('==============RESULTS==============')
print('Best parameters: ', best_param)
print('Best loss: ', -np.min(loss))
print('Time taken: ', time.time() - start)
print('Test accuracy: ', roc_auc_score(yTest, best_clf.predict(xTest)))
print('Parameter combinations evaluated: ', len(trials.trials))
return best_clf, best_param