|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import numpy as np |
|
6
|
|
|
|
|
7
|
|
|
from sklearn.datasets import load_iris |
|
8
|
|
|
from sklearn.model_selection import cross_val_score |
|
9
|
|
|
from sklearn.tree import DecisionTreeClassifier |
|
10
|
|
|
from hyperactive import Hyperactive |
|
11
|
|
|
|
|
12
|
|
|
data = load_iris() |
|
13
|
|
|
X = data.data |
|
14
|
|
|
y = data.target |
|
15
|
|
|
|
|
16
|
|
|
memory = False |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def test_short_term_memory(): |
|
20
|
|
|
def model1(para, X_train, y_train): |
|
21
|
|
|
model = DecisionTreeClassifier(criterion=para["criterion"]) |
|
22
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=5) |
|
23
|
|
|
|
|
24
|
|
|
return scores.mean() |
|
25
|
|
|
|
|
26
|
|
|
search_config = {model1: {"criterion": ["gini"]}} |
|
27
|
|
|
|
|
28
|
|
|
opt = Hyperactive(X, y, memory="short") |
|
29
|
|
|
opt.search(search_config, n_iter=1000) |
|
30
|
|
|
|
|
31
|
|
|
assert np.array(opt.eval_times[model1]).mean() < 1 |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
View Code Duplication |
def test_long_term_memory_with_data(): |
|
|
|
|
|
|
35
|
|
|
def model2(para, X_train, y_train): |
|
36
|
|
|
model = DecisionTreeClassifier( |
|
37
|
|
|
criterion=para["criterion"], max_depth=para["max_depth"] |
|
38
|
|
|
) |
|
39
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
|
40
|
|
|
|
|
41
|
|
|
return scores.mean() |
|
42
|
|
|
|
|
43
|
|
|
search_config = { |
|
44
|
|
|
model2: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
opt = Hyperactive(X, y, memory="long") |
|
48
|
|
|
opt.search(search_config) |
|
49
|
|
|
|
|
50
|
|
|
opt = Hyperactive(X, y, memory="long") |
|
51
|
|
|
opt.search(search_config) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
View Code Duplication |
def test_long_term_memory_without_data(): |
|
|
|
|
|
|
55
|
|
|
def model3(para, X_train, y_train): |
|
56
|
|
|
model = DecisionTreeClassifier( |
|
57
|
|
|
criterion=para["criterion"], max_depth=para["max_depth"] |
|
58
|
|
|
) |
|
59
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
|
60
|
|
|
|
|
61
|
|
|
return scores.mean() |
|
62
|
|
|
|
|
63
|
|
|
search_config = { |
|
64
|
|
|
model3: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
opt = Hyperactive(X, y, memory="long") |
|
68
|
|
|
opt.search(search_config, n_iter=0) |
|
69
|
|
|
|
|
70
|
|
|
opt = Hyperactive(X, y, memory="long") |
|
71
|
|
|
opt.search(search_config) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def test_long_term_memory_best_model(): |
|
75
|
|
|
def model4(para, X_train, y_train): |
|
76
|
|
|
model = DecisionTreeClassifier( |
|
77
|
|
|
criterion=para["criterion"], max_depth=para["max_depth"] |
|
78
|
|
|
) |
|
79
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
|
80
|
|
|
|
|
81
|
|
|
return scores.mean() |
|
82
|
|
|
|
|
83
|
|
|
search_config = { |
|
84
|
|
|
model4: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
opt1 = Hyperactive(X, y, memory="long") |
|
88
|
|
|
opt1.search(search_config) |
|
89
|
|
|
|
|
90
|
|
|
best_para = opt1.results[model4] |
|
91
|
|
|
|
|
92
|
|
|
opt2 = Hyperactive(X, y, memory="long") |
|
93
|
|
|
opt2.search(search_config, n_iter=0, init_config=opt1.results) |
|
94
|
|
|
|
|
95
|
|
|
assert best_para == opt2.results[model4] |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
def test_long_term_memory_obj_storage(): |
|
99
|
|
|
from sklearn.gaussian_process import GaussianProcessClassifier |
|
100
|
|
|
|
|
101
|
|
|
from sklearn.gaussian_process.kernels import RBF, Matern, ConstantKernel |
|
102
|
|
|
|
|
103
|
|
|
def model(para, X_train, y_train): |
|
104
|
|
|
gpc = GaussianProcessClassifier( |
|
105
|
|
|
kernel=para["kernel"], max_iter_predict=para["max_iter_predict"] |
|
106
|
|
|
) |
|
107
|
|
|
scores = cross_val_score(gpc, X_train, y_train, cv=2) |
|
108
|
|
|
|
|
109
|
|
|
return scores.mean() |
|
110
|
|
|
|
|
111
|
|
|
search_config = { |
|
112
|
|
|
model: { |
|
113
|
|
|
"kernel": [RBF(), Matern(), ConstantKernel()], |
|
114
|
|
|
"max_iter_predict": range(90, 100), |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
opt1 = Hyperactive(X, y, memory="long") |
|
119
|
|
|
opt1.search(search_config) |
|
120
|
|
|
|
|
121
|
|
|
best_para = opt1.results[model] |
|
122
|
|
|
|
|
123
|
|
|
opt2 = Hyperactive(X, y, memory="long") |
|
124
|
|
|
opt2.search(search_config, n_iter=0, init_config=opt1.results) |
|
125
|
|
|
|
|
126
|
|
|
assert best_para == opt2.results[model] |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
View Code Duplication |
def test_long_term_memory_search_space_expansion(): |
|
|
|
|
|
|
130
|
|
|
def model5(para, X_train, y_train): |
|
131
|
|
|
model = DecisionTreeClassifier(criterion=para["criterion"]) |
|
132
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
|
133
|
|
|
|
|
134
|
|
|
return scores.mean() |
|
135
|
|
|
|
|
136
|
|
|
search_config = {model5: {"criterion": ["gini", "entropy"]}} |
|
137
|
|
|
|
|
138
|
|
|
opt = Hyperactive(X, y, memory="long") |
|
139
|
|
|
opt.search(search_config) |
|
140
|
|
|
|
|
141
|
|
|
def model5(para, X_train, y_train): |
|
142
|
|
|
model = DecisionTreeClassifier( |
|
143
|
|
|
criterion=para["criterion"], max_depth=para["max_depth"] |
|
144
|
|
|
) |
|
145
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
|
146
|
|
|
|
|
147
|
|
|
return scores.mean() |
|
148
|
|
|
|
|
149
|
|
|
search_config = { |
|
150
|
|
|
model5: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
opt = Hyperactive(X, y, memory="long") |
|
154
|
|
|
opt.search(search_config) |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
View Code Duplication |
def test_long_term_memory_search_space_reduction(): |
|
|
|
|
|
|
158
|
|
|
def model6(para, X_train, y_train): |
|
159
|
|
|
model = DecisionTreeClassifier( |
|
160
|
|
|
criterion=para["criterion"], max_depth=para["max_depth"] |
|
161
|
|
|
) |
|
162
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
|
163
|
|
|
|
|
164
|
|
|
return scores.mean() |
|
165
|
|
|
|
|
166
|
|
|
search_config = { |
|
167
|
|
|
model6: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
opt = Hyperactive(X, y, memory="long") |
|
171
|
|
|
opt.search(search_config) |
|
172
|
|
|
|
|
173
|
|
|
def model6(para, X_train, y_train): |
|
174
|
|
|
model = DecisionTreeClassifier(criterion=para["criterion"]) |
|
175
|
|
|
scores = cross_val_score(model, X_train, y_train, cv=2) |
|
176
|
|
|
|
|
177
|
|
|
return scores.mean() |
|
178
|
|
|
|
|
179
|
|
|
search_config = {model6: {"criterion": ["gini", "entropy"]}} |
|
180
|
|
|
|
|
181
|
|
|
opt = Hyperactive(X, y, memory="long") |
|
182
|
|
|
opt.search(search_config) |
|
183
|
|
|
|