1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
from sklearn.datasets import load_iris |
6
|
|
|
from sklearn.model_selection import cross_val_score |
7
|
|
|
from sklearn.tree import DecisionTreeClassifier |
8
|
|
|
from hyperactive import Hyperactive |
9
|
|
|
|
10
|
|
|
data = load_iris() |
11
|
|
|
X = data.data |
12
|
|
|
y = data.target |
13
|
|
|
|
14
|
|
|
|
15
|
|
View Code Duplication |
def model(para, X, y): |
|
|
|
|
16
|
|
|
model = DecisionTreeClassifier( |
17
|
|
|
criterion=para["criterion"], |
18
|
|
|
max_depth=para["max_depth"], |
19
|
|
|
min_samples_split=para["min_samples_split"], |
20
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
21
|
|
|
) |
22
|
|
|
scores = cross_val_score(model, X, y, cv=3) |
23
|
|
|
|
24
|
|
|
return scores.mean() |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
search_config = { |
28
|
|
|
model: { |
29
|
|
|
"criterion": ["gini", "entropy"], |
30
|
|
|
"max_depth": range(1, 21), |
31
|
|
|
"min_samples_split": range(2, 21), |
32
|
|
|
"min_samples_leaf": range(1, 21), |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
warm_start = { |
37
|
|
|
model: { |
38
|
|
|
"criterion": ["gini"], |
39
|
|
|
"max_depth": [2], |
40
|
|
|
"min_samples_split": [2], |
41
|
|
|
"min_samples_leaf": [2], |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def test_func_return(): |
47
|
|
View Code Duplication |
def model1(para, X, y): |
|
|
|
|
48
|
|
|
model = DecisionTreeClassifier( |
49
|
|
|
criterion=para["criterion"], |
50
|
|
|
max_depth=para["max_depth"], |
51
|
|
|
min_samples_split=para["min_samples_split"], |
52
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
53
|
|
|
) |
54
|
|
|
scores = cross_val_score(model, X, y, cv=3) |
55
|
|
|
|
56
|
|
|
return scores.mean(), model |
57
|
|
|
|
58
|
|
|
search_config1 = { |
59
|
|
|
model1: { |
60
|
|
|
"criterion": ["gini", "entropy"], |
61
|
|
|
"max_depth": range(1, 21), |
62
|
|
|
"min_samples_split": range(2, 21), |
63
|
|
|
"min_samples_leaf": range(1, 21), |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
opt = Hyperactive(search_config1) |
68
|
|
|
opt.search(X, y) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_n_jobs_2(): |
72
|
|
|
opt = Hyperactive(search_config, n_jobs=2) |
73
|
|
|
opt.search(X, y) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_n_jobs_4(): |
77
|
|
|
opt = Hyperactive(search_config, n_jobs=4) |
78
|
|
|
opt.search(X, y) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def test_positional_args(): |
82
|
|
|
opt0 = Hyperactive(search_config, random_state=False) |
83
|
|
|
opt0.search(X, y) |
84
|
|
|
|
85
|
|
|
opt1 = Hyperactive(search_config, random_state=1) |
86
|
|
|
opt1.search(X, y) |
87
|
|
|
|
88
|
|
|
opt2 = Hyperactive(search_config, random_state=1) |
89
|
|
|
opt2.search(X, y) |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def test_random_state(): |
93
|
|
|
opt0 = Hyperactive(search_config, random_state=False) |
94
|
|
|
opt0.search(X, y) |
95
|
|
|
|
96
|
|
|
opt1 = Hyperactive(search_config, random_state=0) |
97
|
|
|
opt1.search(X, y) |
98
|
|
|
|
99
|
|
|
opt2 = Hyperactive(search_config, random_state=1) |
100
|
|
|
opt2.search(X, y) |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
def test_max_time(): |
104
|
|
|
opt0 = Hyperactive(search_config, max_time=0.001) |
105
|
|
|
opt0.search(X, y) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def test_memory(): |
109
|
|
|
opt0 = Hyperactive(search_config, memory=True) |
110
|
|
|
opt0.search(X, y) |
111
|
|
|
|
112
|
|
|
opt1 = Hyperactive(search_config, memory=False) |
113
|
|
|
opt1.search(X, y) |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
def test_verbosity(): |
117
|
|
|
opt0 = Hyperactive(search_config, verbosity=0) |
118
|
|
|
opt0.search(X, y) |
119
|
|
|
|
120
|
|
|
opt0 = Hyperactive(search_config, n_jobs=2, verbosity=0) |
121
|
|
|
opt0.search(X, y) |
122
|
|
|
|
123
|
|
|
opt1 = Hyperactive(search_config, verbosity=1) |
124
|
|
|
opt1.search(X, y) |
125
|
|
|
|
126
|
|
|
opt0 = Hyperactive(search_config, n_jobs=2, verbosity=1) |
127
|
|
|
opt0.search(X, y) |
128
|
|
|
|
129
|
|
|
opt1 = Hyperactive(search_config, verbosity=2) |
130
|
|
|
opt1.search(X, y) |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
def test_scatter_init(): |
134
|
|
|
opt = Hyperactive(search_config, scatter_init=10) |
135
|
|
|
opt.search(X, y) |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
def test_scatter_init_and_warm_start(): |
139
|
|
|
opt = Hyperactive(search_config, warm_start=warm_start, scatter_init=10) |
140
|
|
|
opt.search(X, y) |
141
|
|
|
|
142
|
|
|
opt = Hyperactive(search_config, warm_start=warm_start, scatter_init=10) |
143
|
|
|
opt.search(X, y) |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
def test_warm_start_multiple_jobs(): |
147
|
|
|
opt = Hyperactive(search_config, n_jobs=4, warm_start=warm_start) |
148
|
|
|
opt.search(X, y) |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
def test_warm_start(): |
152
|
|
|
opt = Hyperactive(search_config, n_jobs=1, warm_start=warm_start) |
153
|
|
|
opt.search(X, y) |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
def test_get_search_path(): |
157
|
|
|
opt = Hyperactive(search_config, get_search_path=True) |
158
|
|
|
opt.search(X, y) |
159
|
|
|
|
160
|
|
|
opt = Hyperactive(search_config, optimizer="ParticleSwarm", get_search_path=True) |
161
|
|
|
opt.search(X, y) |
162
|
|
|
|