-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_jostar.py
252 lines (227 loc) · 11.1 KB
/
test_jostar.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 31 14:57:23 2021
@author: Amirh
"""
from sklearn.datasets import make_classification, make_regression
from sklearn.metrics import r2_score
from jostar.algorithms import ACO, GA, SA, PSO, PlusLMinusR, DE, NSGA2, SBS, SFS
from sklearn.svm import SVR, SVC
from sklearn.base import is_classifier, is_regressor
from sklearn.model_selection import KFold
import warnings
import matplotlib.pyplot as plt
import pandas as pd
from tqdm import tqdm
import matplotlib
def eval_opt_model_output_regression(opt_model, n_f):
rank_models = ["GA", "SA", "PSO", "ACO", "DE"]
seq_models = ["PlusLMinusR", "SFS", "SBS"]
if opt_model._name_ in rank_models:
assert len(opt_model.best_fits) == 1
assert len(opt_model.best_sol) == n_f
assert is_regressor(opt_model.model_best)
assert len(opt_model.rankings) == 2
assert opt_model.display_results() is not None
elif opt_model._name_ in seq_models:
if opt_model._name_ != "SBS":
assert len(opt_model.best_fits) == n_f
assert len(opt_model.best_sol) == n_f
assert is_regressor(opt_model.model_best)
assert opt_model.display_results() is not None
else:
assert len(opt_model.best_sol) == n_f
assert is_regressor(opt_model.model_best)
assert opt_model.display_results() is not None
else:
result_df = opt_model.res_df
assert isinstance(result_df, pd.core.frame.DataFrame)
assert opt_model.display_results(0) is not None
plt.close('all')
pbar.update(1)
def eval_opt_model_output_classification(opt_model, n_f):
rank_models = ["GA", "SA", "PSO", "ACO", "DE"]
seq_models = ["PlusLMinusR", "SFS", "SBS"]
if opt_model._name_ in rank_models:
assert len(opt_model.best_fits) == 1
assert len(opt_model.best_sol) == n_f
assert is_classifier(opt_model.model_best)
rank_models = ["GA", "SA", "PSO", "ACO", "DE"]
if opt_model._name_ in rank_models:
assert len(opt_model.rankings) == 2
assert opt_model.display_results() is not None
elif opt_model._name_ in seq_models:
if opt_model._name_ != "SBS":
assert len(opt_model.best_fits) == n_f
assert len(opt_model.best_sol) == n_f
assert is_classifier(opt_model.model_best)
assert opt_model.display_results() is not None
else:
assert len(opt_model.best_sol) == n_f
assert is_classifier(opt_model.model_best)
assert opt_model.display_results() is not None
else:
result_df = opt_model.res_df
assert isinstance(result_df, pd.core.frame.DataFrame)
assert opt_model.display_results(0) is not None
plt.close('all')
pbar.update(1)
def test_all_regression():
global pbar
cv = KFold(5)
n_f = 5
x, y = make_regression(100, 10)
model = SVR()
# regression
# with CV
pbar = tqdm(total=18)
ga_opt_model = GA(model, n_f, +1, r2_score, n_gen=1,
n_pop=20, cv=cv, verbose=False)
sa_opt_model = SA(model, n_f, +1, r2_score, n_iter=1,
n_sub_iter=20, cv=cv, verbose=False)
de_opt_model = DE(model, n_f, +1, r2_score, n_iter=1,
n_pop=20, cv=cv, verbose=False)
aco_opt_model = ACO(model, n_f, +1, r2_score, n_iter=1,
n_ant=20, cv=cv, verbose=False)
pso_opt_model = PSO(model, n_f, +1, r2_score, n_iter=1,
n_pop=20, cv=cv, verbose=False)
lrs_opt_model = PlusLMinusR(model, n_f, +1, r2_score, cv=cv, verbose=False)
nsga_opt_model = NSGA2(model, n_f, (+1, -1), r2_score,
n_gen=1, n_pop=20, cv=cv, verbose=False)
sbs_opt_model = SBS(model, n_f, +1, r2_score, cv=cv, verbose=False)
sfs_opt_model = SFS(model, n_f, +1, r2_score, cv=cv, verbose=False)
ga_opt_model.fit(x, y, decor=0.95, scale=True)
sa_opt_model.fit(x, y, decor=0.95, scale=True)
de_opt_model.fit(x, y, decor=0.95, scale=True)
aco_opt_model.fit(x, y, decor=0.95, scale=True)
pso_opt_model.fit(x, y, decor=0.95, scale=True)
lrs_opt_model.fit(x, y, decor=0.95, scale=True)
nsga_opt_model.fit(x, y, decor=0.95, scale=True)
sbs_opt_model.fit(x, y, decor=0.95, scale=True)
sfs_opt_model.fit(x, y, decor=0.95, scale=True)
eval_opt_model_output_regression(ga_opt_model, n_f)
eval_opt_model_output_regression(sa_opt_model, n_f)
eval_opt_model_output_regression(de_opt_model, n_f)
eval_opt_model_output_regression(aco_opt_model, n_f)
eval_opt_model_output_regression(pso_opt_model, n_f)
eval_opt_model_output_regression(lrs_opt_model, n_f)
eval_opt_model_output_regression(nsga_opt_model, n_f)
eval_opt_model_output_regression(sbs_opt_model, n_f)
eval_opt_model_output_regression(sfs_opt_model, n_f)
# with test size
ga_opt_model = GA(model, n_f, +1, r2_score, n_gen=1,
n_pop=20, cv=None, verbose=False)
sa_opt_model = SA(model, n_f, +1, r2_score, n_iter=1,
n_sub_iter=20, cv=None, verbose=False)
de_opt_model = DE(model, n_f, +1, r2_score, n_iter=1,
n_pop=20, cv=None, verbose=False)
aco_opt_model = ACO(model, n_f, +1, r2_score, n_iter=1,
n_ant=20, cv=None, verbose=False)
pso_opt_model = PSO(model, n_f, +1, r2_score, n_iter=1,
n_pop=20, cv=None, verbose=False)
lrs_opt_model = PlusLMinusR(
model, n_f, +1, r2_score, cv=None, verbose=False)
nsga_opt_model = NSGA2(model, n_f, (+1, -1), r2_score,
n_gen=1, n_pop=20, cv=None, verbose=False)
sbs_opt_model = SBS(model, n_f, +1, r2_score, cv=None, verbose=False)
sfs_opt_model = SFS(model, n_f, +1, r2_score, cv=None, verbose=False)
ga_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
sa_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
de_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
aco_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
pso_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
lrs_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
nsga_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
sbs_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
sfs_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
eval_opt_model_output_regression(ga_opt_model, n_f)
eval_opt_model_output_regression(sa_opt_model, n_f)
eval_opt_model_output_regression(de_opt_model, n_f)
eval_opt_model_output_regression(aco_opt_model, n_f)
eval_opt_model_output_regression(pso_opt_model, n_f)
eval_opt_model_output_regression(lrs_opt_model, n_f)
eval_opt_model_output_regression(nsga_opt_model, n_f)
eval_opt_model_output_regression(sbs_opt_model, n_f)
eval_opt_model_output_regression(sfs_opt_model, n_f)
def test_all_classification():
global pbar
cv = KFold(5)
n_f = 5
x, y = make_classification(100, 10)
model = SVC(probability=True)
# regression
# with CV
pbar = tqdm(total=18)
ga_opt_model = GA(model, n_f, +1, r2_score, n_gen=1,
n_pop=20, cv=cv, verbose=False)
sa_opt_model = SA(model, n_f, +1, r2_score, n_iter=1,
n_sub_iter=20, cv=cv, verbose=False)
de_opt_model = DE(model, n_f, +1, r2_score, n_iter=1,
n_pop=20, cv=cv, verbose=False)
aco_opt_model = ACO(model, n_f, +1, r2_score, n_iter=1,
n_ant=20, cv=cv, verbose=False)
pso_opt_model = PSO(model, n_f, +1, r2_score, n_iter=1,
n_pop=20, cv=cv, verbose=False)
lrs_opt_model = PlusLMinusR(model, n_f, +1, r2_score, cv=cv, verbose=False)
nsga_opt_model = NSGA2(model, n_f, (+1, -1), r2_score,
n_gen=1, n_pop=20, cv=cv, verbose=False)
sbs_opt_model = SBS(model, n_f, +1, r2_score, cv=cv, verbose=False)
sfs_opt_model = SFS(model, n_f, +1, r2_score, cv=cv, verbose=False)
ga_opt_model.fit(x, y, decor=0.95, scale=True)
sa_opt_model.fit(x, y, decor=0.95, scale=True)
de_opt_model.fit(x, y, decor=0.95, scale=True)
aco_opt_model.fit(x, y, decor=0.95, scale=True)
pso_opt_model.fit(x, y, decor=0.95, scale=True)
lrs_opt_model.fit(x, y, decor=0.95, scale=True)
nsga_opt_model.fit(x, y, decor=0.95, scale=True)
sbs_opt_model.fit(x, y, decor=0.95, scale=True)
sfs_opt_model.fit(x, y, decor=0.95, scale=True)
eval_opt_model_output_classification(ga_opt_model, n_f)
eval_opt_model_output_classification(sa_opt_model, n_f)
eval_opt_model_output_classification(de_opt_model, n_f)
eval_opt_model_output_classification(aco_opt_model, n_f)
eval_opt_model_output_classification(pso_opt_model, n_f)
eval_opt_model_output_classification(lrs_opt_model, n_f)
eval_opt_model_output_classification(nsga_opt_model, n_f)
eval_opt_model_output_classification(sbs_opt_model, n_f)
eval_opt_model_output_classification(sfs_opt_model, n_f)
# with test size
ga_opt_model = GA(model, n_f, +1, r2_score, n_gen=1,
n_pop=20, cv=None, verbose=False)
sa_opt_model = SA(model, n_f, +1, r2_score, n_iter=1,
n_sub_iter=20, cv=None, verbose=False)
de_opt_model = DE(model, n_f, +1, r2_score, n_iter=1,
n_pop=20, cv=None, verbose=False)
aco_opt_model = ACO(model, n_f, +1, r2_score, n_iter=1,
n_ant=20, cv=None, verbose=False)
pso_opt_model = PSO(model, n_f, +1, r2_score, n_iter=1,
n_pop=20, cv=None, verbose=False)
lrs_opt_model = PlusLMinusR(
model, n_f, +1, r2_score, cv=None, verbose=False)
nsga_opt_model = NSGA2(model, n_f, (+1, -1), r2_score,
n_gen=1, n_pop=20, cv=None, verbose=False)
sbs_opt_model = SBS(model, n_f, +1, r2_score, cv=None, verbose=False)
sfs_opt_model = SFS(model, n_f, +1, r2_score, cv=None, verbose=False)
ga_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
sa_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
de_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
aco_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
pso_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
lrs_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
nsga_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
sbs_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
sfs_opt_model.fit(x, y, decor=0.95, scale=True, test_size=0.3)
eval_opt_model_output_classification(ga_opt_model, n_f)
eval_opt_model_output_classification(sa_opt_model, n_f)
eval_opt_model_output_classification(de_opt_model, n_f)
eval_opt_model_output_classification(aco_opt_model, n_f)
eval_opt_model_output_classification(pso_opt_model, n_f)
eval_opt_model_output_classification(lrs_opt_model, n_f)
eval_opt_model_output_classification(nsga_opt_model, n_f)
eval_opt_model_output_classification(sbs_opt_model, n_f)
eval_opt_model_output_classification(sfs_opt_model, n_f)
if __name__ == '__main__':
test_all_regression()
test_all_classification()
print(" ")
print("All tests passed!")