PineBioML 1.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PineBioML/IO/__init__.py +158 -0
- PineBioML/__init__.py +0 -0
- PineBioML/model/__init__.py +0 -0
- PineBioML/model/supervised/Classification.py +989 -0
- PineBioML/model/supervised/Regression.py +783 -0
- PineBioML/model/supervised/Survival.py +1 -0
- PineBioML/model/supervised/__init__.py +414 -0
- PineBioML/model/utils.py +393 -0
- PineBioML/preprocessing/__init__.py +168 -0
- PineBioML/preprocessing/bagging.py +221 -0
- PineBioML/preprocessing/impute.py +143 -0
- PineBioML/preprocessing/utils.py +161 -0
- PineBioML/report/__init__.py +0 -0
- PineBioML/report/utils.py +763 -0
- PineBioML/selection/Volcano.py +201 -0
- PineBioML/selection/__init__.py +145 -0
- PineBioML/selection/classification.py +836 -0
- PineBioML/selection/regression.py +737 -0
- PineBioML-1.2.0.dist-info/LICENSE +21 -0
- PineBioML-1.2.0.dist-info/METADATA +148 -0
- PineBioML-1.2.0.dist-info/RECORD +23 -0
- PineBioML-1.2.0.dist-info/WHEEL +5 -0
- PineBioML-1.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,783 @@
|
|
|
1
|
+
from . import Basic_tuner
|
|
2
|
+
from abc import abstractmethod
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
from joblib import parallel_backend
|
|
6
|
+
|
|
7
|
+
from sklearn.model_selection import StratifiedKFold
|
|
8
|
+
|
|
9
|
+
from sklearn.svm import SVR
|
|
10
|
+
from sklearn.ensemble import RandomForestRegressor, AdaBoostRegressor
|
|
11
|
+
from sklearn.tree import DecisionTreeRegressor
|
|
12
|
+
from sklearn.linear_model import ElasticNet
|
|
13
|
+
from xgboost import XGBRegressor
|
|
14
|
+
from lightgbm import LGBMRegressor
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
from statsmodels.regression.linear_model import OLS
|
|
18
|
+
|
|
19
|
+
# Todo: Now loss(not metrics) only support mse, we need more like mae, mape, hinge... etc
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Regression_tuner(Basic_tuner):
|
|
23
|
+
"""
|
|
24
|
+
A subclass of Basic_tuner for regression task.
|
|
25
|
+
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self,
|
|
29
|
+
n_try,
|
|
30
|
+
n_cv,
|
|
31
|
+
target,
|
|
32
|
+
validate_penalty,
|
|
33
|
+
kernel_seed=None,
|
|
34
|
+
valid_seed=None,
|
|
35
|
+
optuna_seed=None):
|
|
36
|
+
super().__init__(n_try=n_try,
|
|
37
|
+
n_cv=n_cv,
|
|
38
|
+
target=target,
|
|
39
|
+
validate_penalty=validate_penalty,
|
|
40
|
+
kernel_seed=kernel_seed,
|
|
41
|
+
valid_seed=valid_seed,
|
|
42
|
+
optuna_seed=optuna_seed)
|
|
43
|
+
"""
|
|
44
|
+
Args:
|
|
45
|
+
n_try (int): The number of trials optuna should try.
|
|
46
|
+
n_cv (int): The number of folds to execute cross validation evaluation in iteration of optuna optimization.
|
|
47
|
+
target (str): The target of optuna optimization. Notice that is different from the training loss of model.
|
|
48
|
+
validate_penalty (bool): True to penalty the overfitting by difference between training score and cv score.
|
|
49
|
+
kernel_seed (int, optional): Random seed for model. Defaults to None.
|
|
50
|
+
valid_seed (int, optional): Random seed for cross validation. Defaults to None.
|
|
51
|
+
optuna_seed (int, optional): Random seed for optuna's hyperparameter sampling. Defaults to None.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def is_regression(self) -> bool:
|
|
55
|
+
"""
|
|
56
|
+
Yes, it is for regression.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
bool: True
|
|
60
|
+
"""
|
|
61
|
+
return True
|
|
62
|
+
|
|
63
|
+
def reference(self) -> dict[str, str]:
|
|
64
|
+
"""
|
|
65
|
+
This function will return reference of this method in python dict.
|
|
66
|
+
If you want to access it in PineBioML api document, then click on the >Expand source code
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
dict[str, str]: a dict of reference.
|
|
70
|
+
"""
|
|
71
|
+
return super().reference()
|
|
72
|
+
|
|
73
|
+
@abstractmethod
|
|
74
|
+
def name(self) -> str:
|
|
75
|
+
"""
|
|
76
|
+
To be determined.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
str: Name of this tuner.
|
|
80
|
+
"""
|
|
81
|
+
pass
|
|
82
|
+
|
|
83
|
+
@abstractmethod
|
|
84
|
+
def create_model(self, trial, default):
|
|
85
|
+
"""
|
|
86
|
+
Create model based on default setting or optuna trial
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
trial (optuna.trial.Trial): optuna trial in this call.
|
|
90
|
+
default (bool): To use default hyper parameter
|
|
91
|
+
|
|
92
|
+
Returns :
|
|
93
|
+
sklearn.base.BaseEstimator: A sklearn style model object.
|
|
94
|
+
"""
|
|
95
|
+
pass
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# linear model, elasticnet
|
|
99
|
+
class ElasticNet_tuner(Regression_tuner):
|
|
100
|
+
"""
|
|
101
|
+
Tuning a elasic net regression.
|
|
102
|
+
[sklearn.linear_model.ElasticNet](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ElasticNet.html)
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
def __init__(self,
|
|
106
|
+
n_try=25,
|
|
107
|
+
n_cv=5,
|
|
108
|
+
target="mse",
|
|
109
|
+
kernel_seed=None,
|
|
110
|
+
valid_seed=None,
|
|
111
|
+
optuna_seed=None,
|
|
112
|
+
validate_penalty=True):
|
|
113
|
+
"""
|
|
114
|
+
Args:
|
|
115
|
+
n_try (int, optional): The number of trials optuna should try. Defaults to 25.
|
|
116
|
+
n_cv (int, optional): The number of folds to execute cross validation evaluation in iteration of optuna optimization. Defaults to 5.
|
|
117
|
+
target (str, optional): The target of optuna optimization. Notice that is different from the training loss of model. Defaults to "mse".
|
|
118
|
+
kernel_seed (int, optional): Random seed for model. Defaults to None.
|
|
119
|
+
valid_seed (int, optional): Random seed for cross validation. Defaults to None.
|
|
120
|
+
optuna_seed (int, optional): Random seed for optuna's hyperparameter sampling. Defaults to None.
|
|
121
|
+
validate_penalty (bool, optional): True to penalty the overfitting by difference between training score and cv score. Defaults to True.
|
|
122
|
+
"""
|
|
123
|
+
super().__init__(n_try=n_try,
|
|
124
|
+
n_cv=n_cv,
|
|
125
|
+
target=target,
|
|
126
|
+
kernel_seed=kernel_seed,
|
|
127
|
+
valid_seed=valid_seed,
|
|
128
|
+
optuna_seed=optuna_seed,
|
|
129
|
+
validate_penalty=validate_penalty)
|
|
130
|
+
|
|
131
|
+
def name(self):
|
|
132
|
+
return "ElasticNet"
|
|
133
|
+
|
|
134
|
+
def reference(self) -> dict[str, str]:
|
|
135
|
+
"""
|
|
136
|
+
This function will return reference of this method in python dict.
|
|
137
|
+
If you want to access it in PineBioML api document, then click on the >Expand source code
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
dict[str, str]: a dict of reference.
|
|
141
|
+
"""
|
|
142
|
+
refer = super().reference()
|
|
143
|
+
refer[
|
|
144
|
+
self.name() +
|
|
145
|
+
" document"] = "https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ElasticNet.html"
|
|
146
|
+
|
|
147
|
+
return refer
|
|
148
|
+
|
|
149
|
+
def create_model(self, trial, default=False):
|
|
150
|
+
if default:
|
|
151
|
+
parms = {}
|
|
152
|
+
else:
|
|
153
|
+
parms = {
|
|
154
|
+
"alpha": trial.suggest_float('alpha', 1e-3, 1e+3, log=True),
|
|
155
|
+
"l1_ratio": trial.suggest_float('l1_ratio', 0, 1),
|
|
156
|
+
}
|
|
157
|
+
enr = ElasticNet(**parms)
|
|
158
|
+
return enr
|
|
159
|
+
|
|
160
|
+
def summary(self):
|
|
161
|
+
"""
|
|
162
|
+
It is the way I found to cram a sklearn regression result into the statsmodel regresion.
|
|
163
|
+
The only reason to do this is that statsmodel provides R-style summary.
|
|
164
|
+
"""
|
|
165
|
+
raise NotImplementedError
|
|
166
|
+
sm_ols = OLS(self.y, self.x).fit_regularized(
|
|
167
|
+
alpha=self.best_model.alpha,
|
|
168
|
+
L1_wt=self.best_model.l1_ratio,
|
|
169
|
+
start_params=self.best_model.coef_.flatten(),
|
|
170
|
+
maxiter=0,
|
|
171
|
+
)
|
|
172
|
+
print(sm_ols.summary())
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
# RF
|
|
176
|
+
class RandomForest_tuner(Regression_tuner):
|
|
177
|
+
"""
|
|
178
|
+
Tuning a random forest model.
|
|
179
|
+
[sklearn.ensemble.RandomForestRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html)
|
|
180
|
+
"""
|
|
181
|
+
|
|
182
|
+
def __init__(self,
|
|
183
|
+
using_oob=True,
|
|
184
|
+
n_try=50,
|
|
185
|
+
n_cv=5,
|
|
186
|
+
target="mse",
|
|
187
|
+
kernel_seed=None,
|
|
188
|
+
valid_seed=None,
|
|
189
|
+
optuna_seed=None,
|
|
190
|
+
validate_penalty=True):
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
using_oob (bool, optional): Using out of bag score as validation. Defaults to True.
|
|
195
|
+
n_try (int, optional): The number of trials optuna should try. Defaults to 50.
|
|
196
|
+
n_cv (int, optional): The number of folds to execute cross validation evaluation in iteration of optuna optimization. Defaults to 5.
|
|
197
|
+
target (str, optional): The target of optuna optimization. Notice that is different from the training loss of model. Defaults to "mse".
|
|
198
|
+
kernel_seed (int, optional): Random seed for model. Defaults to None.
|
|
199
|
+
valid_seed (int, optional): Random seed for cross validation. Defaults to None.
|
|
200
|
+
optuna_seed (int, optional): Random seed for optuna's hyperparameter sampling. Defaults to None.
|
|
201
|
+
validate_penalty (bool, optional): True to penalty the overfitting by difference between training score and cv score. Defaults to True.
|
|
202
|
+
"""
|
|
203
|
+
super().__init__(n_try=n_try,
|
|
204
|
+
n_cv=n_cv,
|
|
205
|
+
target=target,
|
|
206
|
+
kernel_seed=kernel_seed,
|
|
207
|
+
valid_seed=valid_seed,
|
|
208
|
+
optuna_seed=optuna_seed,
|
|
209
|
+
validate_penalty=validate_penalty)
|
|
210
|
+
|
|
211
|
+
self.using_oob = using_oob
|
|
212
|
+
|
|
213
|
+
def name(self):
|
|
214
|
+
return "RandomForest"
|
|
215
|
+
|
|
216
|
+
def reference(self) -> dict[str, str]:
|
|
217
|
+
"""
|
|
218
|
+
This function will return reference of this method in python dict.
|
|
219
|
+
If you want to access it in PineBioML api document, then click on the >Expand source code
|
|
220
|
+
|
|
221
|
+
Returns:
|
|
222
|
+
dict[str, str]: a dict of reference.
|
|
223
|
+
"""
|
|
224
|
+
refer = super().reference()
|
|
225
|
+
refer[
|
|
226
|
+
self.name() +
|
|
227
|
+
" document"] = "https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html"
|
|
228
|
+
refer[
|
|
229
|
+
self.name() +
|
|
230
|
+
" publication"] = "https://www.stat.berkeley.edu/~breiman/randomforest2001.pdf"
|
|
231
|
+
|
|
232
|
+
return refer
|
|
233
|
+
|
|
234
|
+
def create_model(self, trial, default=False):
|
|
235
|
+
if default:
|
|
236
|
+
parms = {
|
|
237
|
+
"bootstrap": self.using_oob,
|
|
238
|
+
"oob_score": self.using_oob,
|
|
239
|
+
"n_jobs": -1,
|
|
240
|
+
"random_state": self.kernel_seed,
|
|
241
|
+
"verbose": 0,
|
|
242
|
+
}
|
|
243
|
+
else:
|
|
244
|
+
parms = {
|
|
245
|
+
"n_estimators":
|
|
246
|
+
trial.suggest_int('n_estimators', 32, 1024, log=True),
|
|
247
|
+
"max_depth":
|
|
248
|
+
trial.suggest_int('max_depth', 4, 16, log=True),
|
|
249
|
+
"min_samples_leaf":
|
|
250
|
+
trial.suggest_int('min_samples_leaf', 1, 32, log=True),
|
|
251
|
+
"ccp_alpha":
|
|
252
|
+
trial.suggest_float('ccp_alpha', 1e-4, 1e-1, log=True),
|
|
253
|
+
"max_samples":
|
|
254
|
+
trial.suggest_float('max_samples', 0.5, 0.9, log=True),
|
|
255
|
+
"bootstrap":
|
|
256
|
+
self.using_oob,
|
|
257
|
+
"oob_score":
|
|
258
|
+
self.using_oob,
|
|
259
|
+
"n_jobs":
|
|
260
|
+
-1,
|
|
261
|
+
"random_state":
|
|
262
|
+
self.kernel_seed_tape[trial.number],
|
|
263
|
+
"verbose":
|
|
264
|
+
0,
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
rf = RandomForestRegressor(**parms)
|
|
268
|
+
return rf
|
|
269
|
+
|
|
270
|
+
def evaluate(self, trial, default=False):
|
|
271
|
+
regressor_obj = self.create_model(trial, default)
|
|
272
|
+
|
|
273
|
+
if self.using_oob:
|
|
274
|
+
# oob predict
|
|
275
|
+
with parallel_backend('loky'):
|
|
276
|
+
regressor_obj.fit(self.x, self.y)
|
|
277
|
+
y_pred = regressor_obj.oob_prediction_
|
|
278
|
+
|
|
279
|
+
# oob score
|
|
280
|
+
score = self.metric._score_func(self.y, y_pred)
|
|
281
|
+
|
|
282
|
+
if not self.metric_great_better:
|
|
283
|
+
# if not great is better, then multiply -1
|
|
284
|
+
score *= -1
|
|
285
|
+
else:
|
|
286
|
+
# cv score
|
|
287
|
+
cv = StratifiedKFold(
|
|
288
|
+
n_splits=self.n_cv,
|
|
289
|
+
shuffle=True,
|
|
290
|
+
random_state=self.valid_seed_tape[trial.number])
|
|
291
|
+
score = []
|
|
292
|
+
|
|
293
|
+
for i, (train_ind, test_ind) in enumerate(cv.split(self.x,
|
|
294
|
+
self.y)):
|
|
295
|
+
x_train = self.x.iloc[train_ind]
|
|
296
|
+
y_train = self.y.iloc[train_ind]
|
|
297
|
+
x_test = self.x.iloc[test_ind]
|
|
298
|
+
y_test = self.y.iloc[test_ind]
|
|
299
|
+
|
|
300
|
+
with parallel_backend('loky'):
|
|
301
|
+
regressor_obj.fit(self.x, self.y)
|
|
302
|
+
|
|
303
|
+
test_score = self.metric(regressor_obj, x_test, y_test)
|
|
304
|
+
train_score = self.metric(regressor_obj, x_train, y_train)
|
|
305
|
+
|
|
306
|
+
if self.validate_penalty:
|
|
307
|
+
score.append(test_score + 0.1 * (test_score - train_score))
|
|
308
|
+
else:
|
|
309
|
+
score.append(test_score)
|
|
310
|
+
|
|
311
|
+
score = sum(score) / self.n_cv
|
|
312
|
+
return score
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
# SVM
|
|
316
|
+
class SVM_tuner(Regression_tuner):
|
|
317
|
+
"""
|
|
318
|
+
Tuning a support vector machine.
|
|
319
|
+
[sklearn.svm.SVR](https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html)
|
|
320
|
+
"""
|
|
321
|
+
|
|
322
|
+
def __init__(self,
|
|
323
|
+
kernel: Literal["linear", "poly", "rbf", "sigmoid"] = "rbf",
|
|
324
|
+
n_try=25,
|
|
325
|
+
n_cv=5,
|
|
326
|
+
target="mse",
|
|
327
|
+
kernel_seed=None,
|
|
328
|
+
valid_seed=None,
|
|
329
|
+
optuna_seed=None,
|
|
330
|
+
validate_penalty=True):
|
|
331
|
+
"""
|
|
332
|
+
Args:
|
|
333
|
+
kernel (Literal["linear", "poly", "rbf", "sigmoid"], optional): This will be passed to the attribute of SVC: "kernel". Defaults to "rbf".
|
|
334
|
+
n_try (int, optional): The number of trials optuna should try. Defaults to 25.
|
|
335
|
+
n_cv (int, optional): The number of folds to execute cross validation evaluation in iteration of optuna optimization. Defaults to 5.
|
|
336
|
+
target (str, optional): The target of optuna optimization. Notice that is different from the training loss of model. Defaults to "mse".
|
|
337
|
+
kernel_seed (int, optional): Random seed for model. Defaults to None.
|
|
338
|
+
valid_seed (int, optional): Random seed for cross validation. Defaults to None.
|
|
339
|
+
optuna_seed (int, optional): Random seed for optuna's hyperparameter sampling. Defaults to None.
|
|
340
|
+
validate_penalty (bool, optional): True to penalty the overfitting by difference between training score and cv score. Defaults to True.
|
|
341
|
+
"""
|
|
342
|
+
super().__init__(n_try=n_try,
|
|
343
|
+
n_cv=n_cv,
|
|
344
|
+
target=target,
|
|
345
|
+
kernel_seed=kernel_seed,
|
|
346
|
+
valid_seed=valid_seed,
|
|
347
|
+
optuna_seed=optuna_seed,
|
|
348
|
+
validate_penalty=validate_penalty)
|
|
349
|
+
self.kernel = kernel
|
|
350
|
+
|
|
351
|
+
def name(self):
|
|
352
|
+
return self.kernel + "-SVM"
|
|
353
|
+
|
|
354
|
+
def reference(self) -> dict[str, str]:
|
|
355
|
+
"""
|
|
356
|
+
This function will return reference of this method in python dict.
|
|
357
|
+
If you want to access it in PineBioML api document, then click on the >Expand source code
|
|
358
|
+
|
|
359
|
+
Returns:
|
|
360
|
+
dict[str, str]: a dict of reference.
|
|
361
|
+
"""
|
|
362
|
+
refer = super().reference()
|
|
363
|
+
refer[
|
|
364
|
+
self.name() +
|
|
365
|
+
" document"] = "https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html"
|
|
366
|
+
refer[
|
|
367
|
+
self.name() +
|
|
368
|
+
" sklearn backend"] = "http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf"
|
|
369
|
+
refer[
|
|
370
|
+
self.name() +
|
|
371
|
+
" publication"] = "https://citeseerx.ist.psu.edu/doc_view/pid/42e5ed832d4310ce4378c44d05570439df28a393"
|
|
372
|
+
|
|
373
|
+
return refer
|
|
374
|
+
|
|
375
|
+
def create_model(self, trial, default=False):
|
|
376
|
+
if default:
|
|
377
|
+
parms = {
|
|
378
|
+
"kernel": self.kernel,
|
|
379
|
+
}
|
|
380
|
+
else:
|
|
381
|
+
# scaling penalty: https://scikit-learn.org/stable/auto_examples/svm/plot_svm_scale_c.html#sphx-glr-auto-examples-svm-plot-svm-scale-c-py
|
|
382
|
+
parms = {
|
|
383
|
+
"C":
|
|
384
|
+
trial.suggest_float('C',
|
|
385
|
+
1e-4 * np.sqrt(self.n_sample),
|
|
386
|
+
1e+2 * np.sqrt(self.n_sample),
|
|
387
|
+
log=True),
|
|
388
|
+
"kernel":
|
|
389
|
+
self.kernel,
|
|
390
|
+
"gamma":
|
|
391
|
+
"auto",
|
|
392
|
+
}
|
|
393
|
+
svm = SVR(**parms)
|
|
394
|
+
return svm
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
# XGboost
|
|
398
|
+
class XGBoost_tuner(Regression_tuner):
|
|
399
|
+
"""
|
|
400
|
+
Tuning a XGBoost classifier model.
|
|
401
|
+
[xgboost.XGBClassifier](https://xgboost.readthedocs.io/en/stable/python/python_api.html)
|
|
402
|
+
|
|
403
|
+
ToDo:
|
|
404
|
+
1. sample imbalance. (Done)
|
|
405
|
+
2. early stop. (give up)
|
|
406
|
+
3. efficiency (optuna.integration.XGBoostPruningCallback).
|
|
407
|
+
|
|
408
|
+
"""
|
|
409
|
+
|
|
410
|
+
def __init__(self,
|
|
411
|
+
n_try=75,
|
|
412
|
+
n_cv=5,
|
|
413
|
+
target="mse",
|
|
414
|
+
kernel_seed=None,
|
|
415
|
+
valid_seed=None,
|
|
416
|
+
optuna_seed=None,
|
|
417
|
+
validate_penalty=True):
|
|
418
|
+
"""
|
|
419
|
+
|
|
420
|
+
Args:
|
|
421
|
+
n_try (int, optional): The number of trials optuna should try. Defaults to 75.
|
|
422
|
+
n_cv (int, optional): The number of folds to execute cross validation evaluation in iteration of optuna optimization. Defaults to 5.
|
|
423
|
+
target (str, optional): The target of optuna optimization. Notice that is different from the training loss of model. Defaults to "mse".
|
|
424
|
+
kernel_seed (int, optional): Random seed for model. Defaults to None.
|
|
425
|
+
valid_seed (int, optional): Random seed for cross validation. Defaults to None.
|
|
426
|
+
optuna_seed (int, optional): Random seed for optuna's hyperparameter sampling. Defaults to None.
|
|
427
|
+
validate_penalty (bool, optional): True to penalty the overfitting by difference between training score and cv score. Defaults to True.
|
|
428
|
+
|
|
429
|
+
"""
|
|
430
|
+
super().__init__(n_try=n_try,
|
|
431
|
+
n_cv=n_cv,
|
|
432
|
+
target=target,
|
|
433
|
+
kernel_seed=kernel_seed,
|
|
434
|
+
valid_seed=valid_seed,
|
|
435
|
+
optuna_seed=optuna_seed,
|
|
436
|
+
validate_penalty=validate_penalty)
|
|
437
|
+
|
|
438
|
+
def name(self):
|
|
439
|
+
return "XGBoost"
|
|
440
|
+
|
|
441
|
+
def reference(self) -> dict[str, str]:
|
|
442
|
+
"""
|
|
443
|
+
This function will return reference of this method in python dict.
|
|
444
|
+
If you want to access it in PineBioML api document, then click on the >Expand source code
|
|
445
|
+
|
|
446
|
+
Returns:
|
|
447
|
+
dict[str, str]: a dict of reference.
|
|
448
|
+
"""
|
|
449
|
+
refer = super().reference()
|
|
450
|
+
refer[self.name() +
|
|
451
|
+
" document"] = "https://xgboost.readthedocs.io/en/stable/"
|
|
452
|
+
refer[
|
|
453
|
+
self.name() +
|
|
454
|
+
" publication"] = "https://dl.acm.org/doi/10.1145/2939672.2939785"
|
|
455
|
+
|
|
456
|
+
return refer
|
|
457
|
+
|
|
458
|
+
def create_model(self, trial, default=False):
|
|
459
|
+
if default:
|
|
460
|
+
parms = {
|
|
461
|
+
"n_jobs": None,
|
|
462
|
+
"random_state": self.kernel_seed,
|
|
463
|
+
"verbosity": 0
|
|
464
|
+
}
|
|
465
|
+
else:
|
|
466
|
+
if trial.suggest_categorical("use_subsample", [True, False]):
|
|
467
|
+
sampling_rate = trial.suggest_float('subsample',
|
|
468
|
+
0.5,
|
|
469
|
+
0.9,
|
|
470
|
+
log=True)
|
|
471
|
+
col_sampling_rate = trial.suggest_float(
|
|
472
|
+
'colsample_bytree', 0.1, 0.9)
|
|
473
|
+
else:
|
|
474
|
+
sampling_rate = 1.
|
|
475
|
+
col_sampling_rate = 1.
|
|
476
|
+
parms = {
|
|
477
|
+
"n_estimators":
|
|
478
|
+
trial.suggest_int('n_estimators', 16, 512, log=True),
|
|
479
|
+
"max_depth":
|
|
480
|
+
trial.suggest_int('max_depth', 2, 16, log=True),
|
|
481
|
+
"gamma":
|
|
482
|
+
trial.suggest_float('gamma', 5e-2, 2e+1, log=True),
|
|
483
|
+
"learning_rate":
|
|
484
|
+
trial.suggest_float('learning_rate', 5e-2, 5e-1, log=True),
|
|
485
|
+
"subsample":
|
|
486
|
+
sampling_rate,
|
|
487
|
+
"colsample_bytree":
|
|
488
|
+
col_sampling_rate,
|
|
489
|
+
#"min_child_weight":
|
|
490
|
+
#trial.suggest_float('min_child_weight', 1e-3, 1e+2, log=True),
|
|
491
|
+
"reg_lambda":
|
|
492
|
+
trial.suggest_float('reg_lambda', 1e-2, 1e+1, log=True),
|
|
493
|
+
"n_jobs":
|
|
494
|
+
None,
|
|
495
|
+
"random_state":
|
|
496
|
+
self.kernel_seed_tape[trial.number],
|
|
497
|
+
"verbosity":
|
|
498
|
+
0,
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
xgb = XGBRegressor(**parms)
|
|
502
|
+
return xgb
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
# lightGBM
|
|
506
|
+
class LighGBM_tuner(Regression_tuner):
|
|
507
|
+
"""
|
|
508
|
+
Tuning a LighGBM classifier model.
|
|
509
|
+
[lightgbm.LGBMClassifier](https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMClassifier.html)
|
|
510
|
+
|
|
511
|
+
ToDo:
|
|
512
|
+
1. compare with optuna.integration.lightgbm.LightGBMTuner
|
|
513
|
+
"""
|
|
514
|
+
|
|
515
|
+
def __init__(self,
|
|
516
|
+
n_try=75,
|
|
517
|
+
n_cv=5,
|
|
518
|
+
target="mse",
|
|
519
|
+
kernel_seed=None,
|
|
520
|
+
valid_seed=None,
|
|
521
|
+
optuna_seed=None,
|
|
522
|
+
validate_penalty=True):
|
|
523
|
+
"""
|
|
524
|
+
|
|
525
|
+
Args:
|
|
526
|
+
n_try (int, optional): The number of trials optuna should try. Defaults to 75.
|
|
527
|
+
n_cv (int, optional): The number of folds to execute cross validation evaluation in iteration of optuna optimization. Defaults to 5.
|
|
528
|
+
target (str, optional): The target of optuna optimization. Notice that is different from the training loss of model. Defaults to "mse".
|
|
529
|
+
kernel_seed (int, optional): Random seed for model. Defaults to None.
|
|
530
|
+
valid_seed (int, optional): Random seed for cross validation. Defaults to None.
|
|
531
|
+
optuna_seed (int, optional): Random seed for optuna's hyperparameter sampling. Defaults to None.
|
|
532
|
+
validate_penalty (bool, optional): True to penalty the overfitting by difference between training score and cv score. Defaults to True.
|
|
533
|
+
|
|
534
|
+
"""
|
|
535
|
+
super().__init__(n_try=n_try,
|
|
536
|
+
n_cv=n_cv,
|
|
537
|
+
target=target,
|
|
538
|
+
kernel_seed=kernel_seed,
|
|
539
|
+
valid_seed=valid_seed,
|
|
540
|
+
optuna_seed=optuna_seed,
|
|
541
|
+
validate_penalty=validate_penalty)
|
|
542
|
+
|
|
543
|
+
def name(self):
|
|
544
|
+
return "LightGBM"
|
|
545
|
+
|
|
546
|
+
def reference(self) -> dict[str, str]:
|
|
547
|
+
"""
|
|
548
|
+
This function will return reference of this method in python dict.
|
|
549
|
+
If you want to access it in PineBioML api document, then click on the >Expand source code
|
|
550
|
+
|
|
551
|
+
Returns:
|
|
552
|
+
dict[str, str]: a dict of reference.
|
|
553
|
+
"""
|
|
554
|
+
refer = super().reference()
|
|
555
|
+
refer[
|
|
556
|
+
self.name() +
|
|
557
|
+
" document"] = "https://lightgbm.readthedocs.io/en/latest/index.html"
|
|
558
|
+
refer[
|
|
559
|
+
self.name() +
|
|
560
|
+
" publication"] = "https://proceedings.neurips.cc/paper_files/paper/2017/file/6449f44a102fde848669bdd9eb6b76fa-Paper.pdf"
|
|
561
|
+
|
|
562
|
+
return refer
|
|
563
|
+
|
|
564
|
+
def create_model(self, trial, default=False):
|
|
565
|
+
if default:
|
|
566
|
+
parms = {
|
|
567
|
+
"n_jobs": None,
|
|
568
|
+
"random_state": self.kernel_seed,
|
|
569
|
+
"verbosity": -1
|
|
570
|
+
}
|
|
571
|
+
else:
|
|
572
|
+
depth = trial.suggest_float('max_depth', 3, 16, log=True)
|
|
573
|
+
leaves = trial.suggest_float('num_leaves',
|
|
574
|
+
depth * 2 / 3,
|
|
575
|
+
depth,
|
|
576
|
+
log=True)
|
|
577
|
+
depth = int(np.rint(depth))
|
|
578
|
+
leaves = int(np.floor(np.power(2, leaves)))
|
|
579
|
+
|
|
580
|
+
if trial.suggest_categorical("use_subsample", [True, False]):
|
|
581
|
+
sampling_rate = trial.suggest_float('subsample',
|
|
582
|
+
0.5,
|
|
583
|
+
0.9,
|
|
584
|
+
log=True)
|
|
585
|
+
col_sampling_rate = trial.suggest_float(
|
|
586
|
+
'colsample_bytree', 0.1, 0.9)
|
|
587
|
+
else:
|
|
588
|
+
sampling_rate = 1.
|
|
589
|
+
col_sampling_rate = 1.
|
|
590
|
+
|
|
591
|
+
parms = {
|
|
592
|
+
"n_estimators":
|
|
593
|
+
trial.suggest_int('n_estimators', 16, 256, log=True),
|
|
594
|
+
"max_depth":
|
|
595
|
+
depth,
|
|
596
|
+
"num_leaves":
|
|
597
|
+
leaves,
|
|
598
|
+
"learning_rate":
|
|
599
|
+
trial.suggest_float('learning_rate', 1e-2, 1, log=True),
|
|
600
|
+
"subsample_freq":
|
|
601
|
+
1,
|
|
602
|
+
"subsample":
|
|
603
|
+
sampling_rate,
|
|
604
|
+
"colsample_bytree":
|
|
605
|
+
col_sampling_rate,
|
|
606
|
+
"min_child_samples":
|
|
607
|
+
trial.suggest_int('min_child_samples', 2, 32, log=True),
|
|
608
|
+
"reg_lambda":
|
|
609
|
+
trial.suggest_float('reg_lambda', 5e-3, 1e+1, log=True),
|
|
610
|
+
"n_jobs":
|
|
611
|
+
None,
|
|
612
|
+
"class_weight":
|
|
613
|
+
"balanced",
|
|
614
|
+
"random_state":
|
|
615
|
+
self.kernel_seed_tape[trial.number],
|
|
616
|
+
"verbosity":
|
|
617
|
+
-1,
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
lgbm = LGBMRegressor(**parms)
|
|
621
|
+
return lgbm
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
# AdaBoost
|
|
625
|
+
class AdaBoost_tuner(Regression_tuner):
|
|
626
|
+
"""
|
|
627
|
+
Tuning a AdaBoost regressor.
|
|
628
|
+
[sklearn.ensemble.AdaBoostRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.AdaBoostRegressor.html)
|
|
629
|
+
"""
|
|
630
|
+
|
|
631
|
+
def __init__(self,
|
|
632
|
+
n_try=25,
|
|
633
|
+
n_cv=5,
|
|
634
|
+
target="mse",
|
|
635
|
+
kernel_seed=None,
|
|
636
|
+
valid_seed=None,
|
|
637
|
+
optuna_seed=None,
|
|
638
|
+
validate_penalty=True):
|
|
639
|
+
"""
|
|
640
|
+
Args:
|
|
641
|
+
n_try (int, optional): The number of trials optuna should try. Defaults to 25.
|
|
642
|
+
n_cv (int, optional): The number of folds to execute cross validation evaluation in iteration of optuna optimization. Defaults to 5.
|
|
643
|
+
target (str, optional): The target of optuna optimization. Notice that is different from the training loss of model. Defaults to "mse".
|
|
644
|
+
kernel_seed (int, optional): Random seed for model. Defaults to None.
|
|
645
|
+
valid_seed (int, optional): Random seed for cross validation. Defaults to None.
|
|
646
|
+
optuna_seed (int, optional): Random seed for optuna's hyperparameter sampling. Defaults to None.
|
|
647
|
+
validate_penalty (bool, optional): True to penalty the overfitting by difference between training score and cv score. Defaults to True.
|
|
648
|
+
"""
|
|
649
|
+
super().__init__(n_try=n_try,
|
|
650
|
+
n_cv=n_cv,
|
|
651
|
+
target=target,
|
|
652
|
+
kernel_seed=kernel_seed,
|
|
653
|
+
valid_seed=valid_seed,
|
|
654
|
+
optuna_seed=optuna_seed,
|
|
655
|
+
validate_penalty=validate_penalty)
|
|
656
|
+
|
|
657
|
+
def name(self):
|
|
658
|
+
return "AdaBoost"
|
|
659
|
+
|
|
660
|
+
def reference(self) -> dict[str, str]:
|
|
661
|
+
"""
|
|
662
|
+
This function will return reference of this method in python dict.
|
|
663
|
+
If you want to access it in PineBioML api document, then click on the >Expand source code
|
|
664
|
+
|
|
665
|
+
Returns:
|
|
666
|
+
dict[str, str]: a dict of reference.
|
|
667
|
+
"""
|
|
668
|
+
refer = super().reference()
|
|
669
|
+
refer[
|
|
670
|
+
self.name() +
|
|
671
|
+
" document"] = "https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.AdaBoostRegressor.html"
|
|
672
|
+
refer[
|
|
673
|
+
self.name() +
|
|
674
|
+
" publication: original version"] = "https://www.ee.columbia.edu/~sfchang/course/svia-F03/papers/freund95decisiontheoretic-adaboost.pdf"
|
|
675
|
+
refer[
|
|
676
|
+
self.name() +
|
|
677
|
+
" publication: implemented version"] = "Drucker, 'Improving Regressors using Boosting Techniques', 1997"
|
|
678
|
+
|
|
679
|
+
return refer
|
|
680
|
+
|
|
681
|
+
def create_model(self, trial, default=False):
|
|
682
|
+
if default:
|
|
683
|
+
parms = {
|
|
684
|
+
"random_state": self.kernel_seed,
|
|
685
|
+
}
|
|
686
|
+
else:
|
|
687
|
+
# scaling penalty: https://scikit-learn.org/stable/auto_examples/svm/plot_svm_scale_c.html#sphx-glr-auto-examples-svm-plot-svm-scale-c-py
|
|
688
|
+
parms = {
|
|
689
|
+
"n_estimators":
|
|
690
|
+
trial.suggest_int('n_estimators', 8, 256, log=True),
|
|
691
|
+
"learning_rate":
|
|
692
|
+
trial.suggest_float('learning_rate', 1e-2, 1, log=True),
|
|
693
|
+
"loss":
|
|
694
|
+
trial.suggest_categorical("loss",
|
|
695
|
+
["linear", "square", "exponential"]),
|
|
696
|
+
"random_state":
|
|
697
|
+
self.kernel_seed_tape[trial.number]
|
|
698
|
+
}
|
|
699
|
+
ada = AdaBoostRegressor(**parms)
|
|
700
|
+
return ada
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
# DT
|
|
704
|
+
class DecisionTree_tuner(Regression_tuner):
|
|
705
|
+
"""
|
|
706
|
+
Tuning a DecisionTree regressor.
|
|
707
|
+
[sklearn.tree.DecisionTreeRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html)
|
|
708
|
+
"""
|
|
709
|
+
|
|
710
|
+
def __init__(self,
|
|
711
|
+
n_try=25,
|
|
712
|
+
n_cv=5,
|
|
713
|
+
target="mse",
|
|
714
|
+
kernel_seed=None,
|
|
715
|
+
valid_seed=None,
|
|
716
|
+
optuna_seed=None,
|
|
717
|
+
validate_penalty=True):
|
|
718
|
+
"""
|
|
719
|
+
Args:
|
|
720
|
+
n_try (int, optional): The number of trials optuna should try. Defaults to 25.
|
|
721
|
+
n_cv (int, optional): The number of folds to execute cross validation evaluation in iteration of optuna optimization. Defaults to 5.
|
|
722
|
+
target (str, optional): The target of optuna optimization. Notice that is different from the training loss of model. Defaults to "mse".
|
|
723
|
+
kernel_seed (int, optional): Random seed for model. Defaults to None.
|
|
724
|
+
valid_seed (int, optional): Random seed for cross validation. Defaults to None.
|
|
725
|
+
optuna_seed (int, optional): Random seed for optuna's hyperparameter sampling. Defaults to None.
|
|
726
|
+
validate_penalty (bool, optional): True to penalty the overfitting by difference between training score and cv score. Defaults to True.
|
|
727
|
+
"""
|
|
728
|
+
super().__init__(n_try=n_try,
|
|
729
|
+
n_cv=n_cv,
|
|
730
|
+
target=target,
|
|
731
|
+
kernel_seed=kernel_seed,
|
|
732
|
+
valid_seed=valid_seed,
|
|
733
|
+
optuna_seed=optuna_seed,
|
|
734
|
+
validate_penalty=validate_penalty)
|
|
735
|
+
|
|
736
|
+
def name(self):
|
|
737
|
+
return "DecisionTree"
|
|
738
|
+
|
|
739
|
+
def reference(self) -> dict[str, str]:
|
|
740
|
+
"""
|
|
741
|
+
This function will return reference of this method in python dict.
|
|
742
|
+
If you want to access it in PineBioML api document, then click on the >Expand source code
|
|
743
|
+
|
|
744
|
+
Returns:
|
|
745
|
+
dict[str, str]: a dict of reference.
|
|
746
|
+
"""
|
|
747
|
+
refer = super().reference()
|
|
748
|
+
refer[
|
|
749
|
+
self.name() +
|
|
750
|
+
" document"] = "https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html"
|
|
751
|
+
refer[
|
|
752
|
+
self.name() +
|
|
753
|
+
" publication"] = "https://www.taylorfrancis.com/books/mono/10.1201/9781315139470/classification-regression-trees-leo-breiman-jerome-friedman-olshen-charles-stone"
|
|
754
|
+
|
|
755
|
+
return refer
|
|
756
|
+
|
|
757
|
+
def create_model(self, trial, default=False):
|
|
758
|
+
if default:
|
|
759
|
+
parms = {
|
|
760
|
+
"random_state": self.kernel_seed,
|
|
761
|
+
}
|
|
762
|
+
else:
|
|
763
|
+
parms = {
|
|
764
|
+
"max_depth":
|
|
765
|
+
trial.suggest_int('max_depth', 2, 16, log=True),
|
|
766
|
+
"min_samples_split":
|
|
767
|
+
trial.suggest_int('min_samples_split', 2, 32, log=True),
|
|
768
|
+
"min_samples_leaf":
|
|
769
|
+
trial.suggest_int('min_samples_leaf', 1, 16, log=True),
|
|
770
|
+
"ccp_alpha":
|
|
771
|
+
trial.suggest_float('ccp_alpha', 1e-3, 1e-1, log=True),
|
|
772
|
+
"random_state":
|
|
773
|
+
self.kernel_seed_tape[trial.number],
|
|
774
|
+
"class_weight":
|
|
775
|
+
"balanced"
|
|
776
|
+
}
|
|
777
|
+
DT = DecisionTreeRegressor(**parms)
|
|
778
|
+
return DT
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
# CatBoost
|
|
782
|
+
# KNN
|
|
783
|
+
# KNN-Graph spectrum
|