Completed
Push — master ( 8b8364...81d861 )
by Simon
04:18
created

test_warm_start_multiple()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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_n_jobs_2():
86
    opt = Hyperactive(X, y, memory=memory)
87
    opt.search(search_config, n_jobs=2)
88
89
90
def test_n_jobs_4():
91
    opt = Hyperactive(X, y, memory=memory)
92
    opt.search(search_config, n_jobs=4)
93
94
95
def test_positional_args():
96
    opt0 = Hyperactive(X, y, random_state=False, memory=memory)
97
    opt0.search(search_config)
98
99
    opt1 = Hyperactive(X, y, random_state=1, memory=memory)
100
    opt1.search(search_config)
101
102
    opt2 = Hyperactive(X, y, random_state=1, memory=memory)
103
    opt2.search(search_config)
104
105
106
def test_random_state():
107
    opt0 = Hyperactive(X, y, random_state=False, memory=memory)
108
    opt0.search(search_config)
109
110
    opt1 = Hyperactive(X, y, random_state=0, memory=memory)
111
    opt1.search(search_config)
112
113
    opt2 = Hyperactive(X, y, random_state=1, memory=memory)
114
    opt2.search(search_config)
115
116
117
def test_max_time():
118
    opt0 = Hyperactive(X, y, memory=memory)
119
    opt0.search(search_config, max_time=0.00001)
120
121
122
def test_memory():
123
    opt0 = Hyperactive(X, y, memory=True)
124
    opt0.search(search_config)
125
126
    opt1 = Hyperactive(X, y, memory=False)
127
    opt1.search(search_config)
128
129
    opt2 = Hyperactive(X, y, memory="short")
130
    opt2.search(search_config)
131
132
    opt3 = Hyperactive(X, y, memory="long")
133
    opt3.search(search_config)
134
135
    opt4 = Hyperactive(X, y, memory="long")
136
    opt4.search(search_config)
137
138
    opt = Hyperactive(X, y, memory=memory, verbosity=0)
139
    opt.search(search_config)
140
141
142
def test_dill():
143
    from sklearn.gaussian_process import GaussianProcessClassifier
144
    from sklearn.gaussian_process.kernels import RBF, Matern
145
146
    def model(para, X, y):
147
        gpc = GaussianProcessClassifier(kernel=para["kernel"])
148
        scores = cross_val_score(gpc, X, y, cv=2)
149
150
        return scores.mean()
151
152
    search_config = {model: {"kernel": [RBF(), Matern()]}}
153
154
    opt0 = Hyperactive(X, y, memory="long")
155
    opt0.search(search_config)
156
157
    opt1 = Hyperactive(X, y, memory="long")
158
    opt1.search(search_config)
159
160
161
def test_verbosity0():
162
    opt = Hyperactive(X, y, verbosity=0, memory=memory)
163
    opt.search(search_config)
164
165
166
def test_verbosity1():
167
    opt = Hyperactive(X, y, verbosity=0, memory=memory)
168
    opt.search(search_config, n_jobs=2)
169
170
171
def test_verbosity2():
172
    opt = Hyperactive(X, y, verbosity=1, memory=memory)
173
    opt.search(search_config, n_jobs=2)
174
175
176
def test_verbosity3():
177
    opt = Hyperactive(X, y, verbosity=1, memory=memory)
178
    opt.search(search_config)
179
180
181
def test_verbosity4():
182
    opt = Hyperactive(X, y, verbosity=2, memory=memory)
183
    opt.search(search_config)
184
185
186
def test_verbosity5():
187
    opt = Hyperactive(X, y, verbosity=2, memory=memory)
188
    opt.search(search_config, n_jobs=2)
189
190
191
def test_scatter_init():
192
    init_config = {model: {"scatter_init": 10}}
193
    opt = Hyperactive(X, y, memory=memory)
194
    opt.search(search_config, init_config=init_config)
195
196
    opt = Hyperactive(X, y, memory=memory, verbosity=0)
197
    opt.search(search_config, init_config=init_config)
198
199
200
def test_warm_start():
201
    init_config = {
202
        model: {"max_depth": 10, "min_samples_split": 2, "min_samples_leaf": 5}
203
    }
204
    opt = Hyperactive(X, y, memory=memory)
205
    opt.search(search_config, n_iter=0, init_config=init_config)
206
207
    assert opt.results[model] == init_config[model]
208
209
210
def test_warm_start_multiple():
211
212
    opt = Hyperactive(X, y, memory="short")
213
    opt.search(search_config, n_iter=10, n_jobs=2)
214
215
216
def test_partial_warm_start():
217
    init_config = {model: {"min_samples_split": 2, "min_samples_leaf": 5}}
218
    opt = Hyperactive(X, y, memory=memory)
219
    opt.search(search_config, n_iter=0, init_config=init_config)
220
221
    opt = Hyperactive(X, y, memory=memory, verbosity=0)
222
    opt.search(search_config, n_iter=0, init_config=init_config)
223
224
225
def test_optimizer_args():
226
    opt = Hyperactive(X, y, memory=memory)
227
    opt.search(search_config, optimizer={"HillClimbing": {"epsilon": 0.1}})
228
229
230
def test_get_search_path():
231
    opt = Hyperactive(X, y, verbosity=10, memory=memory)
232
    opt.search(search_config)
233
234
    opt = Hyperactive(X, y, verbosity=10, memory=memory)
235
    opt.search(search_config, optimizer="ParticleSwarm")
236
237
238
"""
239
def test_ray_1():
240
    ray.init()
241
    opt = Hyperactive(X, y, memory=memory)
242
    opt.search(search_config, n_jobs=1)
243
244
245
def test_ray_2():
246
    ray.init()
247
    opt = Hyperactive(X, y, memory=memory)
248
    opt.search(search_config, n_jobs=2)
249
"""
250