|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
from sklearn.datasets import load_breast_cancer |
|
6
|
|
|
|
|
7
|
|
|
from sklearn.model_selection import cross_val_score |
|
8
|
|
|
from hyperactive import Hyperactive |
|
9
|
|
|
|
|
10
|
|
|
data = load_breast_cancer() |
|
11
|
|
|
X, y = data.data, data.target |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def test_sklearn(): |
|
15
|
|
|
from sklearn.tree import DecisionTreeClassifier |
|
16
|
|
|
|
|
17
|
|
View Code Duplication |
def model(para, X_train, y_train): |
|
|
|
|
|
|
18
|
|
|
model = DecisionTreeClassifier( |
|
19
|
|
|
criterion=para["criterion"], |
|
20
|
|
|
max_depth=para["max_depth"], |
|
21
|
|
|
min_samples_split=para["min_samples_split"], |
|
22
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
|
23
|
|
|
) |
|
24
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=3) |
|
25
|
|
|
|
|
26
|
|
|
return scores.mean() |
|
27
|
|
|
|
|
28
|
|
|
search_config = { |
|
29
|
|
|
model: { |
|
30
|
|
|
"criterion": ["gini", "entropy"], |
|
31
|
|
|
"max_depth": range(1, 21), |
|
32
|
|
|
"min_samples_split": range(2, 21), |
|
33
|
|
|
"min_samples_leaf": range(1, 21), |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
opt = Hyperactive(X, y) |
|
38
|
|
|
opt.search(search_config) |
|
39
|
|
|
# opt.predict(X) |
|
40
|
|
|
# opt.score(X, y) |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
def test_xgboost(): |
|
44
|
|
|
from xgboost import XGBClassifier |
|
45
|
|
|
|
|
46
|
|
|
def model(para, X_train, y_train): |
|
47
|
|
|
model = XGBClassifier( |
|
48
|
|
|
n_estimators=para["n_estimators"], max_depth=para["max_depth"] |
|
49
|
|
|
) |
|
50
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=3) |
|
51
|
|
|
|
|
52
|
|
|
return scores.mean() |
|
53
|
|
|
|
|
54
|
|
|
search_config = {model: {"n_estimators": range(2, 20), "max_depth": range(1, 11)}} |
|
55
|
|
|
|
|
56
|
|
|
opt = Hyperactive(X, y) |
|
57
|
|
|
opt.search(search_config) |
|
58
|
|
|
# opt.predict(X) |
|
59
|
|
|
# opt.score(X, y) |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
def test_lightgbm(): |
|
63
|
|
|
from lightgbm import LGBMClassifier |
|
64
|
|
|
|
|
65
|
|
|
def model(para, X_train, y_train): |
|
66
|
|
|
model = LGBMClassifier( |
|
67
|
|
|
num_leaves=para["num_leaves"], learning_rate=para["learning_rate"] |
|
68
|
|
|
) |
|
69
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=3) |
|
70
|
|
|
|
|
71
|
|
|
return scores.mean() |
|
72
|
|
|
|
|
73
|
|
|
search_config = { |
|
74
|
|
|
model: { |
|
75
|
|
|
"num_leaves": range(2, 20), |
|
76
|
|
|
"learning_rate": [0.001, 0.005, 00.01, 0.05, 0.1, 0.5, 1], |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
opt = Hyperactive(X, y) |
|
81
|
|
|
opt.search(search_config) |
|
82
|
|
|
# opt.predict(X) |
|
83
|
|
|
# opt.score(X, y) |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
def test_catboost(): |
|
87
|
|
|
from catboost import CatBoostClassifier |
|
88
|
|
|
|
|
89
|
|
|
def model(para, X_train, y_train): |
|
90
|
|
|
model = CatBoostClassifier( |
|
91
|
|
|
iterations=para["iterations"], |
|
92
|
|
|
depth=para["depth"], |
|
93
|
|
|
learning_rate=para["learning_rate"], |
|
94
|
|
|
) |
|
95
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=3) |
|
96
|
|
|
|
|
97
|
|
|
return scores.mean() |
|
98
|
|
|
|
|
99
|
|
|
search_config = { |
|
100
|
|
|
model: { |
|
101
|
|
|
"iterations": [1], |
|
102
|
|
|
"depth": range(2, 10), |
|
103
|
|
|
"learning_rate": [0.001, 0.005, 00.01, 0.05, 0.1, 0.5, 1], |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
opt = Hyperactive(X, y) |
|
108
|
|
|
opt.search(search_config) |
|
109
|
|
|
# opt.predict(X) |
|
110
|
|
|
# opt.score(X, y) |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
def test_keras(): |
|
114
|
|
|
from keras.models import Sequential |
|
115
|
|
|
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten |
|
116
|
|
|
from keras.datasets import cifar10 |
|
117
|
|
|
from keras.utils import to_categorical |
|
118
|
|
|
|
|
119
|
|
|
(X_train, y_train), (X_test, y_test) = cifar10.load_data() |
|
120
|
|
|
|
|
121
|
|
|
X_train = X_train[0:1000] |
|
122
|
|
|
y_train = y_train[0:1000] |
|
123
|
|
|
|
|
124
|
|
|
X_test = X_train[0:1000] |
|
125
|
|
|
y_test = y_train[0:1000] |
|
126
|
|
|
|
|
127
|
|
|
y_train = to_categorical(y_train, 10) |
|
128
|
|
|
y_test = to_categorical(y_test, 10) |
|
129
|
|
|
|
|
130
|
|
|
def cnn(para, X_train, y_train): |
|
131
|
|
|
model = Sequential() |
|
132
|
|
|
|
|
133
|
|
|
model.add( |
|
134
|
|
|
Conv2D( |
|
135
|
|
|
filters=para["filters.0"], |
|
136
|
|
|
kernel_size=para["kernel_size.0"], |
|
137
|
|
|
activation="relu", |
|
138
|
|
|
) |
|
139
|
|
|
) |
|
140
|
|
|
model.add(MaxPooling2D(pool_size=(2, 2))) |
|
141
|
|
|
|
|
142
|
|
|
model.add(Flatten()) |
|
143
|
|
|
model.add(Dense(10, activation="softmax")) |
|
144
|
|
|
|
|
145
|
|
|
model.compile( |
|
146
|
|
|
optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] |
|
147
|
|
|
) |
|
148
|
|
|
model.fit(X_train, y_train, epochs=1) |
|
149
|
|
|
|
|
150
|
|
|
_, score = model.evaluate(x=X_test, y=y_test) |
|
151
|
|
|
|
|
152
|
|
|
return score |
|
153
|
|
|
|
|
154
|
|
|
search_config = {cnn: {"filters.0": [32, 64], "kernel_size.0": [3, 4]}} |
|
155
|
|
|
|
|
156
|
|
|
opt = Hyperactive(X_train, y_train) |
|
157
|
|
|
opt.search(search_config) |
|
158
|
|
|
|