1
|
|
|
import numpy as np |
2
|
|
|
from sklearn.datasets import load_breast_cancer |
3
|
|
|
from sklearn.model_selection import cross_val_score |
4
|
|
|
from sklearn.decomposition import PCA |
5
|
|
|
from sklearn.feature_selection import SelectKBest, f_classif |
6
|
|
|
from sklearn.ensemble import GradientBoostingClassifier |
7
|
|
|
from hyperactive import Hyperactive |
8
|
|
|
|
9
|
|
|
data = load_breast_cancer() |
10
|
|
|
X, y = data.data, data.target |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def pca(X): |
14
|
|
|
X = PCA(n_components=10).fit_transform(X) |
15
|
|
|
|
16
|
|
|
return X |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def none(X): |
20
|
|
|
return X |
21
|
|
|
|
22
|
|
|
|
23
|
|
View Code Duplication |
def model(para, X, y): |
|
|
|
|
24
|
|
|
model = GradientBoostingClassifier( |
25
|
|
|
n_estimators=para["n_estimators"], |
26
|
|
|
max_depth=para["max_depth"], |
27
|
|
|
min_samples_split=para["min_samples_split"], |
28
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
X_pca = para["decomposition"](X) |
32
|
|
|
X = np.hstack((X, X_pca)) |
33
|
|
|
|
34
|
|
|
X = SelectKBest(f_classif, k=para["k"]).fit_transform(X, y) |
35
|
|
|
scores = cross_val_score(model, X, y, cv=3) |
36
|
|
|
|
37
|
|
|
return scores.mean() |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
search_config = { |
41
|
|
|
model: { |
42
|
|
|
"decomposition": [pca, none], |
43
|
|
|
"k": range(2, 30), |
44
|
|
|
"n_estimators": range(10, 200, 10), |
45
|
|
|
"max_depth": range(2, 12), |
46
|
|
|
"min_samples_split": range(2, 12), |
47
|
|
|
"min_samples_leaf": range(1, 11), |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
opt = Hyperactive(X, y) |
53
|
|
|
opt.search(search_config, n_iter=100) |
54
|
|
|
|