1
|
|
|
from sklearn.model_selection import cross_val_score |
2
|
|
|
from sklearn.ensemble import GradientBoostingClassifier |
3
|
|
|
from sklearn.datasets import load_breast_cancer |
4
|
|
|
from hyperactive import Hyperactive |
5
|
|
|
|
6
|
|
|
data = load_breast_cancer() |
7
|
|
|
X, y = data.data, data.target |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def model(para, X_train, y_train): |
11
|
|
|
model = GradientBoostingClassifier( |
12
|
|
|
n_estimators=para["n_estimators"], |
13
|
|
|
max_depth=para["max_depth"], |
14
|
|
|
min_samples_split=para["min_samples_split"], |
15
|
|
|
) |
16
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=3) |
17
|
|
|
|
18
|
|
|
return scores.mean(), model |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
# this defines the model and hyperparameter search space |
22
|
|
|
search_config = { |
23
|
|
|
model: { |
24
|
|
|
"n_estimators": range(10, 200, 10), |
25
|
|
|
"max_depth": range(2, 12), |
26
|
|
|
"min_samples_split": range(2, 12), |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
opt = Hyperactive(search_config, n_iter=100, n_jobs=2) |
32
|
|
|
|
33
|
|
|
# search best hyperparameter for given data |
34
|
|
|
opt.fit(X, y) |
35
|
|
|
|