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