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_tensorflow(): |
114
|
|
|
import tensorflow as tf |
115
|
|
|
|
116
|
|
|
mnist = tf.keras.datasets.mnist |
117
|
|
|
|
118
|
|
|
(X_train, y_train), (X_test, y_test) = mnist.load_data() |
119
|
|
|
X_train, X_test = X_train / 255.0, X_test / 255.0 |
120
|
|
|
|
121
|
|
|
def cnn(para, X_train, y_train): |
122
|
|
|
|
123
|
|
|
model = tf.keras.models.Sequential( |
124
|
|
|
[ |
125
|
|
|
tf.keras.layers.Flatten(input_shape=(28, 28)), |
126
|
|
|
tf.keras.layers.Dense(128, activation="relu"), |
127
|
|
|
tf.keras.layers.Dropout(0.2), |
128
|
|
|
tf.keras.layers.Dense(10, activation="softmax"), |
129
|
|
|
] |
130
|
|
|
) |
131
|
|
|
|
132
|
|
|
model.compile( |
133
|
|
|
optimizer="adam", |
134
|
|
|
loss="sparse_categorical_crossentropy", |
135
|
|
|
metrics=["accuracy"], |
136
|
|
|
) |
137
|
|
|
model.fit(X_train, y_train, epochs=1) |
138
|
|
|
|
139
|
|
|
_, score = model.evaluate(X_test, y_test, verbose=2) |
140
|
|
|
print("score", score, type(score)) |
141
|
|
|
return score |
142
|
|
|
|
143
|
|
|
search_config = {cnn: {"filters.0": [32, 64], "kernel_size.0": [3, 4]}} |
144
|
|
|
|
145
|
|
|
opt = Hyperactive(X_train, y_train) |
146
|
|
|
opt.search(search_config) |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
""" |
150
|
|
|
def test_keras(): |
151
|
|
|
from tensorflow.keras.models import Sequential |
152
|
|
|
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten |
153
|
|
|
from tensorflow.keras.datasets import cifar10 |
154
|
|
|
from tensorflow.keras.utils import to_categorical |
155
|
|
|
|
156
|
|
|
(X_train, y_train), (X_test, y_test) = cifar10.load_data() |
157
|
|
|
|
158
|
|
|
X_train = X_train[0:1000] |
159
|
|
|
y_train = y_train[0:1000] |
160
|
|
|
|
161
|
|
|
X_test = X_train[0:1000] |
162
|
|
|
y_test = y_train[0:1000] |
163
|
|
|
|
164
|
|
|
y_train = to_categorical(y_train, 10) |
165
|
|
|
y_test = to_categorical(y_test, 10) |
166
|
|
|
|
167
|
|
|
def cnn(para, X_train, y_train): |
168
|
|
|
model = Sequential() |
169
|
|
|
|
170
|
|
|
model.add( |
171
|
|
|
Conv2D( |
172
|
|
|
filters=para["filters.0"], |
173
|
|
|
kernel_size=para["kernel_size.0"], |
174
|
|
|
activation="relu", |
175
|
|
|
) |
176
|
|
|
) |
177
|
|
|
model.add(MaxPooling2D(pool_size=(2, 2))) |
178
|
|
|
|
179
|
|
|
model.add(Flatten()) |
180
|
|
|
model.add(Dense(10, activation="softmax")) |
181
|
|
|
|
182
|
|
|
model.compile( |
183
|
|
|
optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] |
184
|
|
|
) |
185
|
|
|
model.fit(X_train, y_train, epochs=1) |
186
|
|
|
|
187
|
|
|
_, score = model.evaluate(x=X_test, y=y_test) |
188
|
|
|
|
189
|
|
|
return score |
190
|
|
|
|
191
|
|
|
search_config = {cnn: {"filters.0": [32, 64], "kernel_size.0": [3, 4]}} |
192
|
|
|
|
193
|
|
|
opt = Hyperactive(X_train, y_train) |
194
|
|
|
opt.search(search_config) |
195
|
|
|
""" |
196
|
|
|
|