Completed
Push — master ( 26ba2e...71cdc5 )
by Simon
05:17
created

_test_performance   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 305
Duplicated Lines 81.97 %

Importance

Changes 0
Metric Value
wmc 13
eloc 218
dl 250
loc 305
rs 10
c 0
b 0
f 0

13 Functions

Rating   Name   Duplication   Size   Complexity  
A test_RandomAnnealing() 20 20 1
A model() 10 10 1
A test_RandomSearch() 20 20 1
A test_SimulatedAnnealing() 20 20 1
A test_HillClimbing() 20 20 1
A test_TabuOptimizer() 20 20 1
A test_ParticleSwarm() 20 20 1
A test_ParallelTempering() 20 20 1
A test_EvolutionStrategy() 20 20 1
A test_StochasticTunneling() 20 20 1
A test_StochasticHillClimbing() 20 20 1
A test_RandomRestartHillClimbing() 20 20 1
A test_Bayesian() 20 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
from sklearn.datasets import load_breast_cancer
6
from sklearn.model_selection import cross_val_score
7
from sklearn.tree import DecisionTreeClassifier
8
from hyperactive import Hyperactive
9
10
data = load_breast_cancer()
11
X = data.data
12
y = data.target
13
14
random_state = 1
15
n_iter_min = 0
16
n_iter_max = 100
17
18
19 View Code Duplication
def model(para, X_train, y_train):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
    model = DecisionTreeClassifier(
21
        criterion=para["criterion"],
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(model, X_train, y_train, cv=2)
27
28
    return scores.mean()
29
30
31
search_config = {
32
    model: {
33
        "criterion": ["gini", "entropy"],
34
        "max_depth": range(1, 21),
35
        "min_samples_split": range(2, 21),
36
        "min_samples_leaf": range(1, 21),
37
    }
38
}
39
40
warm_start = {model: {"max_depth": [1]}}
41
42
43 View Code Duplication
def test_HillClimbing():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
44
    opt0 = Hyperactive(
45
        search_config,
46
        optimizer="HillClimbing",
47
        n_iter=n_iter_min,
48
        random_state=random_state,
49
        warm_start=warm_start,
50
    )
51
    opt0.search(X, y)
52
53
    opt1 = Hyperactive(
54
        search_config,
55
        optimizer="HillClimbing",
56
        n_iter=n_iter_max,
57
        random_state=random_state,
58
        warm_start=warm_start,
59
    )
60
    opt1.search(X, y)
61
62
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
63
64
65 View Code Duplication
def test_StochasticHillClimbing():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
66
    opt0 = Hyperactive(
67
        search_config,
68
        optimizer="StochasticHillClimbing",
69
        n_iter=n_iter_min,
70
        random_state=random_state,
71
        warm_start=warm_start,
72
    )
73
    opt0.search(X, y)
74
75
    opt1 = Hyperactive(
76
        search_config,
77
        optimizer="StochasticHillClimbing",
78
        n_iter=n_iter_max,
79
        random_state=random_state,
80
        warm_start=warm_start,
81
    )
82
    opt1.search(X, y)
83
84
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
85
86
87 View Code Duplication
def test_TabuOptimizer():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
88
    opt0 = Hyperactive(
89
        search_config,
90
        optimizer="TabuSearch",
91
        n_iter=n_iter_min,
92
        random_state=random_state,
93
        warm_start=warm_start,
94
    )
95
    opt0.search(X, y)
96
97
    opt1 = Hyperactive(
98
        search_config,
99
        optimizer="TabuSearch",
100
        n_iter=n_iter_max,
101
        random_state=random_state,
102
        warm_start=warm_start,
103
    )
104
    opt1.search(X, y)
105
106
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
107
108
109 View Code Duplication
def test_RandomSearch():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
110
    opt0 = Hyperactive(
111
        search_config,
112
        optimizer="RandomSearch",
113
        n_iter=n_iter_min,
114
        random_state=random_state,
115
        warm_start=warm_start,
116
    )
117
    opt0.search(X, y)
118
119
    opt1 = Hyperactive(
120
        search_config,
121
        optimizer="RandomSearch",
122
        n_iter=n_iter_max,
123
        random_state=random_state,
124
        warm_start=warm_start,
125
    )
126
    opt1.search(X, y)
127
128
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
129
130
131 View Code Duplication
def test_RandomRestartHillClimbing():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
132
    opt0 = Hyperactive(
133
        search_config,
134
        optimizer="RandomRestartHillClimbing",
135
        n_iter=n_iter_min,
136
        random_state=random_state,
137
        warm_start=warm_start,
138
    )
139
    opt0.search(X, y)
140
141
    opt1 = Hyperactive(
142
        search_config,
143
        optimizer="RandomRestartHillClimbing",
144
        n_iter=n_iter_max,
145
        random_state=random_state,
146
        warm_start=warm_start,
147
    )
148
    opt1.search(X, y)
149
150
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
151
152
153 View Code Duplication
def test_RandomAnnealing():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
154
    opt0 = Hyperactive(
155
        search_config,
156
        optimizer="RandomAnnealing",
157
        n_iter=n_iter_min,
158
        random_state=random_state,
159
        warm_start=warm_start,
160
    )
161
    opt0.search(X, y)
162
163
    opt1 = Hyperactive(
164
        search_config,
165
        optimizer="RandomAnnealing",
166
        n_iter=n_iter_max,
167
        random_state=random_state,
168
        warm_start=warm_start,
169
    )
170
    opt1.search(X, y)
171
172
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
173
174
175 View Code Duplication
def test_SimulatedAnnealing():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
176
    opt0 = Hyperactive(
177
        search_config,
178
        optimizer="SimulatedAnnealing",
179
        n_iter=n_iter_min,
180
        random_state=random_state,
181
        warm_start=warm_start,
182
    )
183
    opt0.search(X, y)
184
185
    opt1 = Hyperactive(
186
        search_config,
187
        optimizer="SimulatedAnnealing",
188
        n_iter=n_iter_max,
189
        random_state=random_state,
190
        warm_start=warm_start,
191
    )
192
    opt1.search(X, y)
193
194
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
195
196
197 View Code Duplication
def test_StochasticTunneling():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
198
    opt0 = Hyperactive(
199
        search_config,
200
        optimizer="StochasticTunneling",
201
        n_iter=n_iter_min,
202
        random_state=random_state,
203
        warm_start=warm_start,
204
    )
205
    opt0.search(X, y)
206
207
    opt1 = Hyperactive(
208
        search_config,
209
        optimizer="StochasticTunneling",
210
        n_iter=n_iter_max,
211
        random_state=random_state,
212
        warm_start=warm_start,
213
    )
214
    opt1.search(X, y)
215
216
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
217
218
219 View Code Duplication
def test_ParallelTempering():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
220
    opt0 = Hyperactive(
221
        search_config,
222
        optimizer="ParallelTempering",
223
        n_iter=n_iter_min,
224
        random_state=random_state,
225
        warm_start=warm_start,
226
    )
227
    opt0.search(X, y)
228
229
    opt1 = Hyperactive(
230
        search_config,
231
        optimizer="ParallelTempering",
232
        n_iter=n_iter_max,
233
        random_state=random_state,
234
        warm_start=warm_start,
235
    )
236
    opt1.search(X, y)
237
238
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
239
240
241 View Code Duplication
def test_ParticleSwarm():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
242
    opt0 = Hyperactive(
243
        search_config,
244
        optimizer="ParticleSwarm",
245
        n_iter=n_iter_min,
246
        random_state=random_state,
247
        warm_start=warm_start,
248
    )
249
    opt0.search(X, y)
250
251
    opt1 = Hyperactive(
252
        search_config,
253
        optimizer="ParticleSwarm",
254
        n_iter=n_iter_max,
255
        random_state=random_state,
256
        warm_start=warm_start,
257
    )
258
    opt1.search(X, y)
259
260
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
261
262
263 View Code Duplication
def test_EvolutionStrategy():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
264
    opt0 = Hyperactive(
265
        search_config,
266
        optimizer="EvolutionStrategy",
267
        n_iter=n_iter_min,
268
        random_state=random_state,
269
        warm_start=warm_start,
270
    )
271
    opt0.search(X, y)
272
273
    opt1 = Hyperactive(
274
        search_config,
275
        optimizer="EvolutionStrategy",
276
        n_iter=n_iter_max,
277
        random_state=random_state,
278
        warm_start=warm_start,
279
    )
280
    opt1.search(X, y)
281
282
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
283
284
285 View Code Duplication
def test_Bayesian():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
286
    opt0 = Hyperactive(
287
        search_config,
288
        optimizer="Bayesian",
289
        n_iter=n_iter_min,
290
        random_state=random_state,
291
        warm_start=warm_start,
292
    )
293
    opt0.search(X, y)
294
295
    opt1 = Hyperactive(
296
        search_config,
297
        optimizer="Bayesian",
298
        n_iter=n_iter_max,
299
        random_state=random_state,
300
        warm_start=warm_start,
301
    )
302
    opt1.search(X, y)
303
304
    assert opt0._optimizer_.score_best < opt1._optimizer_.score_best
305