test_early_stop_1()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

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