1
|
|
|
import numpy as np |
2
|
|
|
|
3
|
|
|
from sklearn.model_selection import cross_val_score |
4
|
|
|
from sklearn.ensemble import GradientBoostingClassifier |
5
|
|
|
from sklearn.ensemble import RandomForestClassifier |
6
|
|
|
from sklearn.datasets import load_breast_cancer |
7
|
|
|
|
8
|
|
|
from hyperactive import Hyperactive |
9
|
|
|
from hyperactive.optimizers import ( |
10
|
|
|
HillClimbingOptimizer, |
11
|
|
|
RandomRestartHillClimbingOptimizer, |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
data = load_breast_cancer() |
15
|
|
|
X, y = data.data, data.target |
16
|
|
|
|
17
|
|
|
|
18
|
|
View Code Duplication |
def model_rfc(opt): |
|
|
|
|
19
|
|
|
rfc = RandomForestClassifier( |
20
|
|
|
n_estimators=opt["n_estimators"], |
21
|
|
|
criterion=opt["criterion"], |
22
|
|
|
max_features=opt["max_features"], |
23
|
|
|
min_samples_split=opt["min_samples_split"], |
24
|
|
|
min_samples_leaf=opt["min_samples_leaf"], |
25
|
|
|
bootstrap=opt["bootstrap"], |
26
|
|
|
) |
27
|
|
|
scores = cross_val_score(rfc, X, y, cv=3) |
28
|
|
|
|
29
|
|
|
return scores.mean() |
30
|
|
|
|
31
|
|
|
|
32
|
|
View Code Duplication |
def model_gbc(opt): |
|
|
|
|
33
|
|
|
gbc = GradientBoostingClassifier( |
34
|
|
|
n_estimators=opt["n_estimators"], |
35
|
|
|
learning_rate=opt["learning_rate"], |
36
|
|
|
max_depth=opt["max_depth"], |
37
|
|
|
min_samples_split=opt["min_samples_split"], |
38
|
|
|
min_samples_leaf=opt["min_samples_leaf"], |
39
|
|
|
subsample=opt["subsample"], |
40
|
|
|
max_features=opt["max_features"], |
41
|
|
|
) |
42
|
|
|
scores = cross_val_score(gbc, X, y, cv=3) |
43
|
|
|
|
44
|
|
|
return scores.mean() |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
search_space_rfc = { |
48
|
|
|
"n_estimators": list(range(10, 200, 10)), |
49
|
|
|
"criterion": ["gini", "entropy"], |
50
|
|
|
"max_features": list(np.arange(0.05, 1.01, 0.05)), |
51
|
|
|
"min_samples_split": list(range(2, 21)), |
52
|
|
|
"min_samples_leaf": list(range(1, 21)), |
53
|
|
|
"bootstrap": [True, False], |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
search_space_gbc = { |
58
|
|
|
"n_estimators": list(range(10, 200, 10)), |
59
|
|
|
"learning_rate": [1e-3, 1e-2, 1e-1, 0.5, 1.0], |
60
|
|
|
"max_depth": list(range(1, 11)), |
61
|
|
|
"min_samples_split": list(range(2, 21)), |
62
|
|
|
"min_samples_leaf": list(range(1, 21)), |
63
|
|
|
"subsample": list(np.arange(0.05, 1.01, 0.05)), |
64
|
|
|
"max_features": list(np.arange(0.05, 1.01, 0.05)), |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
optimizer1 = HillClimbingOptimizer() |
68
|
|
|
optimizer2 = RandomRestartHillClimbingOptimizer() |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
hyper = Hyperactive() |
72
|
|
|
hyper.add_search( |
73
|
|
|
model_rfc, |
74
|
|
|
search_space_rfc, |
75
|
|
|
n_iter=50, |
76
|
|
|
optimizer=optimizer1, |
77
|
|
|
) |
78
|
|
|
hyper.add_search( |
79
|
|
|
model_gbc, |
80
|
|
|
search_space_gbc, |
81
|
|
|
n_iter=50, |
82
|
|
|
optimizer=optimizer2, |
83
|
|
|
n_jobs=2, |
84
|
|
|
) |
85
|
|
|
hyper.run(max_time=5) |
86
|
|
|
|