Passed
Push — master ( 7dd267...7b482c )
by Simon
01:23
created

tests.test_hyperactive_api.test_n_jobs_4()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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