sklearn_pipeline_example   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 3

3 Functions

Rating   Name   Duplication   Size   Complexity  
A pipeline2() 0 2 1
A pipeline1() 0 2 1
A model() 0 13 1
1
from sklearn.datasets import load_breast_cancer
2
from sklearn.model_selection import cross_val_score
3
from sklearn.feature_selection import SelectKBest, f_classif
4
from sklearn.ensemble import GradientBoostingClassifier
5
from sklearn.pipeline import Pipeline
6
7
from hyperactive import Hyperactive
8
9
data = load_breast_cancer()
10
X, y = data.data, data.target
11
12
13
def pipeline1(filter_, gbc):
14
    return Pipeline([("filter_", filter_), ("gbc", gbc)])
15
16
17
def pipeline2(filter_, gbc):
18
    return gbc
19
20
21
def model(opt):
22
    gbc = GradientBoostingClassifier(
23
        n_estimators=opt["n_estimators"],
24
        max_depth=opt["max_depth"],
25
        min_samples_split=opt["min_samples_split"],
26
        min_samples_leaf=opt["min_samples_leaf"],
27
    )
28
    filter_ = SelectKBest(f_classif, k=opt["k"])
29
    model_ = opt["pipeline"](filter_, gbc)
30
31
    scores = cross_val_score(model_, X, y, cv=3)
32
33
    return scores.mean()
34
35
36
search_space = {
37
    "k": list(range(2, 30)),
38
    "n_estimators": list(range(10, 200, 10)),
39
    "max_depth": list(range(2, 12)),
40
    "min_samples_split": list(range(2, 12)),
41
    "min_samples_leaf": list(range(1, 11)),
42
    "pipeline": [pipeline1, pipeline2],
43
}
44
45
46
hyper = Hyperactive()
47
hyper.add_search(model, search_space, n_iter=30)
48
hyper.run()
49