1
|
|
|
import numpy as np |
2
|
|
|
from sklearn.model_selection import cross_val_score |
3
|
|
|
from sklearn.tree import DecisionTreeClassifier |
4
|
|
|
from sklearn.datasets import load_breast_cancer |
5
|
|
|
from hyperactive import Hyperactive |
6
|
|
|
|
7
|
|
|
data = load_breast_cancer() |
8
|
|
|
X, y = data.data, data.target |
9
|
|
|
|
10
|
|
|
|
11
|
|
View Code Duplication |
def meta_opt(para, X, y): |
|
|
|
|
12
|
|
|
def model(para, X, y): |
13
|
|
|
model = DecisionTreeClassifier( |
14
|
|
|
max_depth=para["max_depth"], |
15
|
|
|
min_samples_split=para["min_samples_split"], |
16
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
17
|
|
|
) |
18
|
|
|
scores = cross_val_score(model, X, y, cv=3) |
19
|
|
|
|
20
|
|
|
return scores.mean() |
21
|
|
|
|
22
|
|
|
search_config = { |
23
|
|
|
model: { |
24
|
|
|
"max_depth": range(2, 50), |
25
|
|
|
"min_samples_split": range(2, 50), |
26
|
|
|
"min_samples_leaf": range(1, 50), |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
opt = Hyperactive( |
31
|
|
|
search_config, |
32
|
|
|
optimizer={ |
33
|
|
|
"ParticleSwarm": { |
34
|
|
|
"inertia": para["inertia"], |
35
|
|
|
"cognitive_weight": para["cognitive_weight"], |
36
|
|
|
"social_weight": para["social_weight"], |
37
|
|
|
} |
38
|
|
|
}, |
39
|
|
|
verbosity=None, |
40
|
|
|
) |
41
|
|
|
opt.search(X, y) |
42
|
|
|
|
43
|
|
|
return opt.score_best |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
search_config = { |
47
|
|
|
meta_opt: { |
48
|
|
|
"inertia": np.arange(0, 1, 0.01), |
49
|
|
|
"cognitive_weight": np.arange(0, 1, 0.01), |
50
|
|
|
"social_weight": np.arange(0, 1, 0.01), |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
opt = Hyperactive(X, y) |
55
|
|
|
opt.search(search_config, optimizer="Bayesian", n_iter=50) |
56
|
|
|
|