1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import numpy as np |
6
|
|
|
|
7
|
|
|
from sklearn.datasets import load_iris |
8
|
|
|
from sklearn.model_selection import cross_val_score |
9
|
|
|
from sklearn.tree import DecisionTreeClassifier |
10
|
|
|
from sklearn.ensemble import GradientBoostingClassifier |
11
|
|
|
|
12
|
|
|
from hyperactive import Hyperactive |
13
|
|
|
|
14
|
|
|
data = load_iris() |
15
|
|
|
X = data.data |
16
|
|
|
y = data.target |
17
|
|
|
memory = False |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def model(para, X, y): |
21
|
|
|
dtc = DecisionTreeClassifier( |
22
|
|
|
max_depth=para["max_depth"], |
23
|
|
|
min_samples_split=para["min_samples_split"], |
24
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
25
|
|
|
) |
26
|
|
|
scores = cross_val_score(dtc, X, y, cv=2) |
27
|
|
|
|
28
|
|
|
return scores.mean() |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
search_config = { |
32
|
|
|
model: { |
33
|
|
|
"max_depth": range(1, 21), |
34
|
|
|
"min_samples_split": range(2, 21), |
35
|
|
|
"min_samples_leaf": range(1, 21), |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def model0(para, X_train, y_train): |
41
|
|
|
model = DecisionTreeClassifier(criterion=para["criterion"]) |
42
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
43
|
|
|
|
44
|
|
|
return scores.mean() |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def model1(para, X_train, y_train): |
48
|
|
|
model = GradientBoostingClassifier(n_estimators=para["n_estimators"]) |
49
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
50
|
|
|
|
51
|
|
|
return scores.mean() |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
search_config_2 = { |
55
|
|
|
model0: {"criterion": ["gini"]}, |
56
|
|
|
model1: {"n_estimators": range(10, 100)}, |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def test_func_return(): |
61
|
|
|
def model1(para, X, y): |
62
|
|
|
dtc = DecisionTreeClassifier( |
63
|
|
|
criterion=para["criterion"], |
64
|
|
|
max_depth=para["max_depth"], |
65
|
|
|
min_samples_split=para["min_samples_split"], |
66
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
67
|
|
|
) |
68
|
|
|
scores = cross_val_score(dtc, X, y, cv=3) |
69
|
|
|
|
70
|
|
|
return scores.mean(), model |
71
|
|
|
|
72
|
|
|
search_config1 = { |
73
|
|
|
model1: { |
74
|
|
|
"criterion": ["gini", "entropy"], |
75
|
|
|
"max_depth": range(1, 21), |
76
|
|
|
"min_samples_split": range(2, 21), |
77
|
|
|
"min_samples_leaf": range(1, 21), |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
opt = Hyperactive(X, y, memory=memory) |
82
|
|
|
opt.search(search_config1) |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def test_opt_times(): |
86
|
|
|
opt = Hyperactive(X, y, memory=memory) |
87
|
|
|
opt.search(search_config) |
88
|
|
|
|
89
|
|
|
assert np.array(opt.opt_times[model]).mean() > 0 |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def test_eval_times(): |
93
|
|
|
opt = Hyperactive(X, y, memory=memory) |
94
|
|
|
opt.search(search_config) |
95
|
|
|
|
96
|
|
|
assert np.array(opt.eval_times[model]).mean() > 0 |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def test_n_jobs_2(): |
100
|
|
|
opt = Hyperactive(X, y, memory=memory) |
101
|
|
|
opt.search(search_config, n_jobs=2) |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def test_n_jobs_4(): |
105
|
|
|
opt = Hyperactive(X, y, memory=memory) |
106
|
|
|
opt.search(search_config, n_jobs=4) |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def test_positional_args(): |
110
|
|
|
opt0 = Hyperactive(X, y, random_state=False, memory=memory) |
111
|
|
|
opt0.search(search_config) |
112
|
|
|
|
113
|
|
|
opt1 = Hyperactive(X, y, random_state=1, memory=memory) |
114
|
|
|
opt1.search(search_config) |
115
|
|
|
|
116
|
|
|
opt2 = Hyperactive(X, y, random_state=1, memory=memory) |
117
|
|
|
opt2.search(search_config) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
def test_random_state(): |
121
|
|
|
opt0 = Hyperactive(X, y, random_state=False, memory=memory) |
122
|
|
|
opt0.search(search_config) |
123
|
|
|
|
124
|
|
|
opt1 = Hyperactive(X, y, random_state=0, memory=memory) |
125
|
|
|
opt1.search(search_config) |
126
|
|
|
|
127
|
|
|
opt2 = Hyperactive(X, y, random_state=1, memory=memory) |
128
|
|
|
opt2.search(search_config) |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
def test_max_time(): |
132
|
|
|
opt0 = Hyperactive(X, y, memory=memory) |
133
|
|
|
opt0.search(search_config, max_time=0.00001) |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
def test_memory(): |
137
|
|
|
opt0 = Hyperactive(X, y, memory=True) |
138
|
|
|
opt0.search(search_config) |
139
|
|
|
|
140
|
|
|
opt1 = Hyperactive(X, y, memory=False) |
141
|
|
|
opt1.search(search_config) |
142
|
|
|
|
143
|
|
|
opt2 = Hyperactive(X, y, memory="short") |
144
|
|
|
opt2.search(search_config) |
145
|
|
|
|
146
|
|
|
opt3 = Hyperactive(X, y, memory="long") |
147
|
|
|
opt3.search(search_config) |
148
|
|
|
|
149
|
|
|
opt4 = Hyperactive(X, y, memory="long") |
150
|
|
|
opt4.search(search_config) |
151
|
|
|
|
152
|
|
|
opt = Hyperactive(X, y, memory=memory, verbosity=0) |
153
|
|
|
opt.search(search_config) |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
def test_dill(): |
157
|
|
|
from sklearn.gaussian_process import GaussianProcessClassifier |
158
|
|
|
from sklearn.gaussian_process.kernels import RBF, Matern |
159
|
|
|
|
160
|
|
|
def model(para, X, y): |
161
|
|
|
gpc = GaussianProcessClassifier(kernel=para["kernel"]) |
162
|
|
|
scores = cross_val_score(gpc, X, y, cv=2) |
163
|
|
|
|
164
|
|
|
return scores.mean() |
165
|
|
|
|
166
|
|
|
search_config = {model: {"kernel": [RBF(), Matern()]}} |
167
|
|
|
|
168
|
|
|
opt0 = Hyperactive(X, y, memory="long") |
169
|
|
|
opt0.search(search_config) |
170
|
|
|
|
171
|
|
|
opt1 = Hyperactive(X, y, memory="long") |
172
|
|
|
opt1.search(search_config) |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
def test_verbosity0(): |
176
|
|
|
opt = Hyperactive(X, y, verbosity=0, memory=memory) |
177
|
|
|
opt.search(search_config) |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
def test_verbosity1(): |
181
|
|
|
opt = Hyperactive(X, y, verbosity=0, memory=memory) |
182
|
|
|
opt.search(search_config, n_jobs=2) |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
def test_verbosity2(): |
186
|
|
|
opt = Hyperactive(X, y, verbosity=1, memory=memory) |
187
|
|
|
opt.search(search_config, n_jobs=2) |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
def test_verbosity3(): |
191
|
|
|
opt = Hyperactive(X, y, verbosity=1, memory=memory) |
192
|
|
|
opt.search(search_config) |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
def test_verbosity4(): |
196
|
|
|
opt = Hyperactive(X, y, verbosity=2, memory=memory) |
197
|
|
|
opt.search(search_config) |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
def test_verbosity5(): |
201
|
|
|
opt = Hyperactive(X, y, verbosity=2, memory=memory) |
202
|
|
|
opt.search(search_config, n_jobs=2) |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
def test_scatter_init(): |
206
|
|
|
init_config = {model: {"scatter_init": 10}} |
207
|
|
|
opt = Hyperactive(X, y, memory=memory) |
208
|
|
|
opt.search(search_config, init_config=init_config) |
209
|
|
|
|
210
|
|
|
opt = Hyperactive(X, y, memory=memory, verbosity=0) |
211
|
|
|
opt.search(search_config, init_config=init_config) |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
def test_warm_start(): |
215
|
|
|
init_config = { |
216
|
|
|
model: {"max_depth": 10, "min_samples_split": 2, "min_samples_leaf": 5} |
217
|
|
|
} |
218
|
|
|
opt = Hyperactive(X, y, memory=memory) |
219
|
|
|
opt.search(search_config, n_iter=0, init_config=init_config) |
220
|
|
|
|
221
|
|
|
assert opt.results[model] == init_config[model] |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
def test_warm_start_multiple(): |
225
|
|
|
|
226
|
|
|
opt = Hyperactive(X, y, memory="short") |
227
|
|
|
opt.search(search_config, n_iter=10, n_jobs=2) |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
def test_partial_warm_start(): |
231
|
|
|
init_config = {model: {"min_samples_split": 2, "min_samples_leaf": 5}} |
232
|
|
|
opt = Hyperactive(X, y, memory=memory) |
233
|
|
|
opt.search(search_config, n_iter=0, init_config=init_config) |
234
|
|
|
|
235
|
|
|
opt = Hyperactive(X, y, memory=memory, verbosity=0) |
236
|
|
|
opt.search(search_config, n_iter=0, init_config=init_config) |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
def test_optimizer_args(): |
240
|
|
|
opt = Hyperactive(X, y, memory=memory) |
241
|
|
|
opt.search(search_config, optimizer={"HillClimbing": {"epsilon": 0.1}}) |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
def test_get_search_path(): |
245
|
|
|
opt = Hyperactive(X, y, verbosity=10, memory=memory) |
246
|
|
|
opt.search(search_config) |
247
|
|
|
|
248
|
|
|
opt = Hyperactive(X, y, verbosity=10, memory=memory) |
249
|
|
|
opt.search(search_config, optimizer="ParticleSwarm") |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
""" |
253
|
|
|
def test_ray_1(): |
254
|
|
|
ray.init() |
255
|
|
|
opt = Hyperactive(X, y, memory=memory) |
256
|
|
|
opt.search(search_config, n_jobs=1) |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
def test_ray_2(): |
260
|
|
|
ray.init() |
261
|
|
|
opt = Hyperactive(X, y, memory=memory) |
262
|
|
|
opt.search(search_config, n_jobs=2) |
263
|
|
|
""" |
264
|
|
|
|