1
|
|
|
from sklearn.model_selection import cross_val_score |
2
|
|
|
from sklearn.ensemble import GradientBoostingClassifier |
3
|
|
|
from sklearn.datasets import load_iris |
4
|
|
|
from hyperactive import Hyperactive |
5
|
|
|
|
6
|
|
|
iris_data = load_iris() |
7
|
|
|
X = iris_data.data |
8
|
|
|
y = iris_data.target |
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
|
|
|
search_config = { |
21
|
|
|
model: { |
22
|
|
|
"n_estimators": range(10, 200, 10), |
23
|
|
|
"max_depth": range(2, 12), |
24
|
|
|
"min_samples_split": range(2, 12), |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
# Without scatter initialization |
29
|
|
|
opt = SimulatedAnnealingOptimizer( |
|
|
|
|
30
|
|
|
search_config, optimizer="HillClimbing", n_iter=20, random_state=0, scatter_init=False |
31
|
|
|
) |
32
|
|
|
opt.fit(X, y) |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
# With scatter initialization |
36
|
|
|
opt = Hyperactive( |
37
|
|
|
search_config, optimizer="HillClimbing", n_iter=20, random_state=0, scatter_init=True |
38
|
|
|
) |
39
|
|
|
opt.fit(X, y) |
40
|
|
|
|