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