Completed
Push — master ( 75ac4c...d2135c )
by Simon
12:39
created

tests.test_hyperactive_api.test_dill()   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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