Passed
Push — master ( ef01f2...fd3757 )
by Simon
01:46 queued 10s
created

Meta-Optimization   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 56
Duplicated Lines 58.93 %

Importance

Changes 0
Metric Value
wmc 1
eloc 38
dl 33
loc 56
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A meta_opt() 33 33 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
import numpy as np
2
from sklearn.model_selection import cross_val_score
3
from sklearn.tree import DecisionTreeClassifier
4
from sklearn.datasets import load_breast_cancer
5
from hyperactive import Hyperactive
6
7
data = load_breast_cancer()
8
X, y = data.data, data.target
9
10
11 View Code Duplication
def meta_opt(para, X, y):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
12
    def model(para, X, y):
13
        model = DecisionTreeClassifier(
14
            max_depth=para["max_depth"],
15
            min_samples_split=para["min_samples_split"],
16
            min_samples_leaf=para["min_samples_leaf"],
17
        )
18
        scores = cross_val_score(model, X, y, cv=3)
19
20
        return scores.mean()
21
22
    search_config = {
23
        model: {
24
            "max_depth": range(2, 50),
25
            "min_samples_split": range(2, 50),
26
            "min_samples_leaf": range(1, 50),
27
        }
28
    }
29
30
    opt = Hyperactive(
31
        search_config,
32
        optimizer={
33
            "ParticleSwarm": {
34
                "inertia": para["inertia"],
35
                "cognitive_weight": para["cognitive_weight"],
36
                "social_weight": para["social_weight"],
37
            }
38
        },
39
        verbosity=None,
40
    )
41
    opt.search(X, y)
42
43
    return opt.score_best
44
45
46
search_config = {
47
    meta_opt: {
48
        "inertia": np.arange(0, 1, 0.01),
49
        "cognitive_weight": np.arange(0, 1, 0.01),
50
        "social_weight": np.arange(0, 1, 0.01),
51
    }
52
}
53
54
opt = Hyperactive(X, y)
55
opt.search(search_config, optimizer="Bayesian", n_iter=50)
56