-
Notifications
You must be signed in to change notification settings - Fork 42
/
sklearn_pipeline_example.py
48 lines (34 loc) · 1.21 KB
/
sklearn_pipeline_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.pipeline import Pipeline
from hyperactive import Hyperactive
data = load_breast_cancer()
X, y = data.data, data.target
def pipeline1(filter_, gbc):
return Pipeline([("filter_", filter_), ("gbc", gbc)])
def pipeline2(filter_, gbc):
return gbc
def model(opt):
gbc = GradientBoostingClassifier(
n_estimators=opt["n_estimators"],
max_depth=opt["max_depth"],
min_samples_split=opt["min_samples_split"],
min_samples_leaf=opt["min_samples_leaf"],
)
filter_ = SelectKBest(f_classif, k=opt["k"])
model_ = opt["pipeline"](filter_, gbc)
scores = cross_val_score(model_, X, y, cv=3)
return scores.mean()
search_space = {
"k": list(range(2, 30)),
"n_estimators": list(range(10, 200, 10)),
"max_depth": list(range(2, 12)),
"min_samples_split": list(range(2, 12)),
"min_samples_leaf": list(range(1, 11)),
"pipeline": [pipeline1, pipeline2],
}
hyper = Hyperactive()
hyper.add_search(model, search_space, n_iter=30)
hyper.run()