-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
271 lines (230 loc) · 10 KB
/
models.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
# this file provides all models
class Classifier:
"""
this class contains several classifiers with automatic parameter tuning
INPUTs
Xtrain: train data
ytrain: train labels
Xtest: test data
ytest: test labels
"""
# necessary libraries
from xgboost import XGBClassifier
# import lightgbm as lgb # has problem to load module
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
import optuna
import sklearn.model_selection
import xgboost as xgb
from sklearn import metrics
import numpy as np
import pdb
def __init__(self, Xtrain, ytrain, Xtest, ytest, n_jobs=-1):
# initializing class and feeding train test data and labels
self.Xtrain = Xtrain
self.ytrain = ytrain
self.Xtest = Xtest
self.ytest = ytest
# defining computational cores
self.n_jobs = n_jobs
def run_xgboost(self, n_trials=100):
# running xgb classifier and automatically optimizing paramters
study = self.optuna.create_study(direction="maximize")
study.optimize(self.xgb_objective, n_trials=n_trials, timeout=600, n_jobs=self.n_jobs)
print("Number of finished trials: ", len(study.trials))
print("Best trial:")
trial = study.best_trial
print(" Value: {}".format(trial.value))
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))
return study, self.xgb.XGBClassifier(**trial.params)
def run_lgb(self):
# has problem to load module
pass
def run_svc(self, n_trials=100):
# running xgb classifier and automatically optimizing paramters
study = self.optuna.create_study(direction="maximize")
study.optimize(self.svc_objective, n_trials=n_trials, timeout=600, n_jobs=self.n_jobs)
print("Number of finished trials: ", len(study.trials))
print("Best trial:")
trial = study.best_trial
print(" Value: {}".format(trial.value))
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))
return study, self.SVC(**trial.params)
def run_rfc(self, n_trials=100):
# running xgb classifier and automatically optimizing paramters
study = self.optuna.create_study(direction="maximize")
study.optimize(self.rfc_objective, n_trials=n_trials, timeout=600, n_jobs=self.n_jobs)
print("Number of finished trials: ", len(study.trials))
print("Best trial:")
trial = study.best_trial
print(" Value: {}".format(trial.value))
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))
return study, self.RandomForestClassifier(**trial.params)
def run_adaBoost(self, n_trials=100):
# running xgb classifier and automatically optimizing paramters
study = self.optuna.create_study(direction="maximize")
study.optimize(self.ada_objective, n_trials=n_trials, timeout=600, n_jobs=self.n_jobs)
print("Number of finished trials: ", len(study.trials))
print("Best trial:")
trial = study.best_trial
print(" Value: {}".format(trial.value))
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))
return study, self.AdaBoostClassifier(**trial.params)
def rfc_objective(self, trial):
# define parameters
param = {
# n_estimators
"n_estimators": trial.suggest_int("n_estimators", 20, 1000, 20),
# criterion
"criterion": trial.suggest_categorical("criterion", ["gini", "entropy"]),
# min_samples_split
"min_samples_split": trial.suggest_float("min_samples_split", 0.01, 0.5),
# min_samples_leaf
"min_samples_leaf": trial.suggest_float("min_samples_leaf", 0.01, 0.5),
# parallel
"n_jobs": self.n_jobs,
# class_weight
"class_weight": trial.suggest_categorical('class_weight', ["balanced", "balanced_subsample"]),
# complexity paramter
"ccp_alpha": trial.suggest_float("ccp_alpha", 1e-6, 1., log=True),
# max sample
"max_samples": trial.suggest_float("max_samples", .5, 1)
}
# fitting model
brfc = self.RandomForestClassifier(
**param).fit(self.Xtrain, self.ytrain)
preds = brfc.predict(self.Xtest)
# self.pdb.set_trace()
pred_labels = self.np.rint(preds)
try:
accuracy = self.metrics.f1_score(
self.ytest, pred_labels, average='weighted')
except:
accuracy = 0.1
return accuracy
def ada_objective(self, trial):
# define parameters
param = {
# number of estimators
"n_estimators": trial.suggest_int("n_estimators", 10, 1000, 20),
# learning rate
"learning_rate": trial.suggest_float("learning_rate", 1e-1, 1e1, log=True),
# parallel CHECK
"n_jobs": self.n_jobs,
# algorithm
"algorithm": trial.suggest_categorical("algorithm", ["SAMME", "SAMME.R"]),
}
# fitting model
try:
bada = self.AdaBoostClassifier(
**param).fit(self.Xtrain, self.ytrain)
preds = bada.predict(self.Xtest)
# self.pdb.set_trace()
pred_labels = self.np.rint(preds)
try:
accuracy = self.metrics.f1_score(
self.ytest, pred_labels, average='weighted')
except:
accuracy = 0.1
except:
accuracy = 0.1
return accuracy
def svc_objective(self, trial):
# define parameters
param = {
# positive regularization parameter
"C": trial.suggest_float("C", 1e-3, 1e4, log=True),
# kernel type
"kernel": trial.suggest_categorical("kernel", ["linear", "poly", "rbf", "sigmoid"]),
# parallel CHECK
"n_jobs": self.n_jobs,
# degree
"degree": trial.suggest_int("degree", 2, 7),
# kernel coefficient
"gamma": "auto",
# size of kernel
"cache_size": 500,
# class weight
"class_weight": "balanced",
# decision function
"decision_function_shape": trial.suggest_categorical("decision_function_shape", ["ovo", "ovr"]),
}
# fitting model
bclf = self.SVC(**param).fit(self.Xtrain, self.ytrain)
preds = bclf.predict(self.Xtest)
# self.pdb.set_trace()
pred_labels = self.np.rint(preds)
try:
accuracy = self.metrics.f1_score(
self.ytest, pred_labels, average='weighted')
except:
accuracy = 0.1
return accuracy
def xgb_objective(self, trial):
# prepare train/test data and transoform them in xgb Matrix format
dtrain = self.xgb.DMatrix(self.Xtrain, label=self.ytrain)
dvalid = self.xgb.DMatrix(self.Xtest, label=self.ytest)
# define parameter space
param = {
"verbosity": 0,
# "objective": "multi:softmax",
# use exact for small dataset.
"tree_method": "exact",
# defines booster, gblinear for linear functions.
"booster": trial.suggest_categorical("booster", ["gbtree", "gblinear", "dart"]),
# L2 regularization weight.
"lambda": trial.suggest_float("lambda", 1e-8, 1.0, log=True),
# L1 regularization weight.
"alpha": trial.suggest_float("alpha", 1e-8, 1.0, log=True),
# parallel
# "nthread": self.n_jobs,
# sampling ratio for training data.
"subsample": trial.suggest_float("subsample", 0.2, 1.0),
# sampling according to each tree.
"colsample_bytree": trial.suggest_float("colsample_bytree", 0.2, 1.0),
# evaluation metric
"eval_metric": trial.suggest_categorical("eval_metric", ['merror', 'mlogloss']),
# number of classes
# "num_class": len(self.np.unique(self.ytrain)),
}
if param["booster"] in ["gbtree", "dart"]:
# maximum depth of the tree, signifies complexity of the tree.
param["max_depth"] = trial.suggest_int("max_depth", 3, 50, step=2)
# minimum child weight, larger the term more conservative the tree.
param["min_child_weight"] = trial.suggest_int(
"min_child_weight", 2, 20)
param["eta"] = trial.suggest_float("eta", 1e-8, 1.0, log=True)
# defines how selective algorithm is.
param["gamma"] = trial.suggest_float("gamma", 1e-8, 1.0, log=True)
param["grow_policy"] = trial.suggest_categorical(
"grow_policy", ["depthwise", "lossguide"])
if param["booster"] == "dart":
param["sample_type"] = trial.suggest_categorical(
"sample_type", ["uniform", "weighted"])
param["normalize_type"] = trial.suggest_categorical(
"normalize_type", ["tree", "forest"])
param["rate_drop"] = trial.suggest_float(
"rate_drop", 1e-8, 1.0, log=True)
param["skip_drop"] = trial.suggest_float(
"skip_drop", 1e-8, 1.0, log=True)
bst = self.xgb.train(param, dtrain)
preds = bst.predict(dvalid)
# self.pdb.set_trace()
pred_labels = self.np.rint(preds)
try:
accuracy = self.metrics.f1_score(
self.ytest, pred_labels, average='weighted')
except:
accuracy = 0.1
return accuracy
# https://towardsdatascience.com/multi-class-metrics-made-simple-part-ii-the-f1-score-ebe8b2c2ca1
# accuracy metrics: F1, ROC AUC
# https://neptune.ai/blog/hyperparameter-tuning-in-python-a-complete-guide-2020