Passed
Push — master ( 8df986...d057bb )
by Simon
01:28 queued 11s
created

test_early_stop_6()   B

Complexity

Conditions 1

Size

Total Lines 56
Code Lines 45

Duplication

Lines 56
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 45
nop 1
dl 56
loc 56
rs 8.8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import time
2
import pytest
3
import numpy as np
4
from sklearn.datasets import load_breast_cancer
5
from sklearn.model_selection import cross_val_score
6
from sklearn.tree import DecisionTreeClassifier
7
8
from ._parametrize import optimizers
9
10
11
def objective_function(para):
12
    score = -para["x1"] * para["x1"]
13
    return score
14
15
16
search_space = {
17
    "x1": np.arange(0, 10, 0.1),
18
}
19
20
21
@pytest.mark.parametrize(*optimizers)
22
def test_early_stop_0(Optimizer):
23
    early_stopping = {
24
        "n_iter_no_change": 5,
25
        "tol_abs": 0.1,
26
        "tol_rel": 0.1,
27
    }
28
29
    opt = Optimizer(search_space, initialize={"warm_start": [{"x1": 0}]})
30
    opt.search(
31
        objective_function,
32
        n_iter=1000,
33
        early_stopping=early_stopping,
34
    )
35
36
37
@pytest.mark.parametrize(*optimizers)
38
def test_early_stop_1(Optimizer):
39
    early_stopping = {
40
        "n_iter_no_change": 5,
41
        "tol_abs": None,
42
        "tol_rel": 5,
43
    }
44
45
    opt = Optimizer(search_space, initialize={"warm_start": [{"x1": 0}]})
46
    opt.search(
47
        objective_function,
48
        n_iter=1000,
49
        early_stopping=early_stopping,
50
    )
51
52
53
@pytest.mark.parametrize(*optimizers)
54
def test_early_stop_2(Optimizer):
55
    early_stopping = {
56
        "n_iter_no_change": 5,
57
        "tol_abs": 0.1,
58
        "tol_rel": None,
59
    }
60
61
    opt = Optimizer(search_space, initialize={"warm_start": [{"x1": 0}]})
62
    opt.search(
63
        objective_function,
64
        n_iter=1000,
65
        early_stopping=early_stopping,
66
    )
67
68
69 View Code Duplication
@pytest.mark.parametrize(*optimizers)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
70
def test_early_stop_3(Optimizer):
71
    def objective_function(para):
72
        score = -para["x1"] * para["x1"]
73
        return score
74
75
    search_space = {
76
        "x1": np.arange(0, 100, 0.1),
77
    }
78
79
    n_iter_no_change = 5
80
    early_stopping = {
81
        "n_iter_no_change": n_iter_no_change,
82
    }
83
84
    opt = Optimizer(search_space, initialize={"warm_start": [{"x1": 0}]})
85
    opt.search(
86
        objective_function,
87
        n_iter=100000,
88
        early_stopping=early_stopping,
89
    )
90
    search_data = opt.results
91
    n_performed_iter = len(search_data)
92
93
    print("\n n_performed_iter \n", n_performed_iter)
94
    print("\n n_iter_no_change \n", n_iter_no_change)
95
96
    assert n_performed_iter == (n_iter_no_change + 1)
97
98
99 View Code Duplication
@pytest.mark.parametrize(*optimizers)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
100
def test_early_stop_4(Optimizer):
101
    def objective_function(para):
102
        return para["x1"]
103
104
    search_space = {
105
        "x1": np.arange(0, 100, 0.1),
106
    }
107
108
    n_iter_no_change = 5
109
    early_stopping = {
110
        "n_iter_no_change": 5,
111
        "tol_abs": 0.1,
112
        "tol_rel": None,
113
    }
114
115
    start1 = {"x1": 0}
116
    start2 = {"x1": 0.1}
117
    start3 = {"x1": 0.2}
118
    start4 = {"x1": 0.3}
119
    start5 = {"x1": 0.4}
120
121
    warm_start_l = [
122
        start1,
123
        start1,
124
        start1,
125
        start1,
126
        start1,
127
        start2,
128
        start2,
129
        start2,
130
        start3,
131
        start3,
132
        start3,
133
        start4,
134
        start4,
135
        start4,
136
        start5,
137
        start5,
138
        start5,
139
    ]
140
    n_iter = len(warm_start_l)
141
142
    opt = Optimizer(search_space, initialize={"warm_start": warm_start_l})
143
    opt.search(
144
        objective_function,
145
        n_iter=n_iter,
146
        early_stopping=early_stopping,
147
    )
148
    search_data = opt.results
149
    n_performed_iter = len(search_data)
150
151
    print("\n n_performed_iter \n", n_performed_iter)
152
    print("\n n_iter_no_change \n", n_iter_no_change)
153
154
    assert n_performed_iter == n_iter
155
156
157 View Code Duplication
@pytest.mark.parametrize(*optimizers)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
158
def test_early_stop_5(Optimizer):
159
    def objective_function(para):
160
        return para["x1"]
161
162
    search_space = {
163
        "x1": np.arange(0, 100, 0.01),
164
    }
165
166
    n_iter_no_change = 5
167
    early_stopping = {
168
        "n_iter_no_change": n_iter_no_change,
169
        "tol_abs": 0.1,
170
        "tol_rel": None,
171
    }
172
173
    start1 = {"x1": 0}
174
    start2 = {"x1": 0.09}
175
    start3 = {"x1": 0.20}
176
177
    warm_start_l = [
178
        start1,
179
        start1,
180
        start1,
181
        start1,
182
        start1,
183
        start2,
184
        start2,
185
        start2,
186
        start3,
187
        start3,
188
        start3,
189
    ]
190
    n_iter = len(warm_start_l)
191
192
    opt = Optimizer(search_space, initialize={"warm_start": warm_start_l})
193
    opt.search(
194
        objective_function,
195
        n_iter=n_iter,
196
        early_stopping=early_stopping,
197
    )
198
    search_data = opt.results
199
    n_performed_iter = len(search_data)
200
201
    print("\n n_performed_iter \n", n_performed_iter)
202
    print("\n n_iter_no_change \n", n_iter_no_change)
203
204
    assert n_performed_iter == (n_iter_no_change + 1)
205
206
207 View Code Duplication
@pytest.mark.parametrize(*optimizers)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
208
def test_early_stop_6(Optimizer):
209
    def objective_function(para):
210
        return para["x1"]
211
212
    search_space = {
213
        "x1": np.arange(0, 100, 0.01),
214
    }
215
216
    n_iter_no_change = 5
217
    early_stopping = {
218
        "n_iter_no_change": 5,
219
        "tol_abs": None,
220
        "tol_rel": 10,
221
    }
222
223
    start1 = {"x1": 1}
224
    start2 = {"x1": 1.1}
225
    start3 = {"x1": 1.22}
226
    start4 = {"x1": 1.35}
227
    start5 = {"x1": 1.48}
228
229
    warm_start_l = [
230
        start1,
231
        start1,
232
        start1,
233
        start1,
234
        start1,
235
        start2,
236
        start2,
237
        start2,
238
        start3,
239
        start3,
240
        start3,
241
        start4,
242
        start4,
243
        start4,
244
        start5,
245
        start5,
246
        start5,
247
    ]
248
    n_iter = len(warm_start_l)
249
250
    opt = Optimizer(search_space, initialize={"warm_start": warm_start_l})
251
    opt.search(
252
        objective_function,
253
        n_iter=n_iter,
254
        early_stopping=early_stopping,
255
    )
256
    search_data = opt.results
257
    n_performed_iter = len(search_data)
258
259
    print("\n n_performed_iter \n", n_performed_iter)
260
    print("\n n_iter_no_change \n", n_iter_no_change)
261
262
    assert n_performed_iter == n_iter
263
264
265 View Code Duplication
@pytest.mark.parametrize(*optimizers)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
266
def test_early_stop_7(Optimizer):
267
    def objective_function(para):
268
        return para["x1"]
269
270
    search_space = {
271
        "x1": np.arange(0, 100, 0.01),
272
    }
273
274
    n_iter_no_change = 5
275
    early_stopping = {
276
        "n_iter_no_change": n_iter_no_change,
277
        "tol_abs": None,
278
        "tol_rel": 10,
279
    }
280
281
    start1 = {"x1": 1}
282
    start2 = {"x1": 1.09}
283
    start3 = {"x1": 1.20}
284
285
    warm_start_l = [
286
        start1,
287
        start1,
288
        start1,
289
        start1,
290
        start1,
291
        start2,
292
        start2,
293
        start2,
294
        start3,
295
        start3,
296
        start3,
297
    ]
298
    n_iter = len(warm_start_l)
299
300
    opt = Optimizer(search_space, initialize={"warm_start": warm_start_l})
301
    opt.search(
302
        objective_function,
303
        n_iter=n_iter,
304
        early_stopping=early_stopping,
305
    )
306
    search_data = opt.results
307
    n_performed_iter = len(search_data)
308
309
    print("\n n_performed_iter \n", n_performed_iter)
310
    print("\n n_iter_no_change \n", n_iter_no_change)
311
312
    assert n_performed_iter == (n_iter_no_change + 1)
313