-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_selection.py
447 lines (356 loc) · 15.4 KB
/
feature_selection.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
"""
Experiment to compare our method as feature selection approach.
The experiment is performed for binary classification problem.
"""
import sys
import numpy as np
import warnings
import time
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn import tree
from sklearn.feature_selection import RFE
from sklearn.model_selection import StratifiedKFold
from sklearn.feature_selection import mutual_info_classif
from sklearn.svm import LinearSVC
from sklearn.feature_selection import f_classif
import numpy as np
import pandas as pd
import random
# from PyImpetus import PPIMBC
from mrmr import mrmr_classif
sys.path.append('./Main/')
from main import mrc_cg
from mrc_lp_gurobi import mrc_lp_model_gurobi
sys.path.append('./')
sys.path.append('./Datasets/')
from load import *
sys.path.append('./')
sys.path.append('./Libraries/')
from myRandomPhi import *
sys.path.append('./')
sys.path.append('./Libraries/L1SVM_CG/')
from Our_methods import *
from Benchmarks import *
sys.path.append('./')
sys.path.append('./Libraries/CCM/')
import ccm
sys.path.append('./')
import random
def predict_proba(phi_te, mu, nu, n_classes):
"""
Predict the classification probabilities of 0-1 MRC for the given solution.
Parameters
----------
phi_te : array-like of shape (no_of_instances, n_classes, no_of_features)
One-hot encoded feature mappings of the testing data.
mu : array-like of shape (no_of_features)
Solution of the classifier.
nu : float
Solution of the classifier.
Returns
-------
hy_x : array-like of shape (no_of_instances, n_classes)
Prediction probabilities of the given testing data.
"""
hy_x = np.clip(1 + np.dot(phi_te, mu) + nu, 0., None)
c = np.sum(hy_x, axis=1)
# check when the sum is zero
zeros = np.isclose(c, 0)
c[zeros] = 1
hy_x[zeros, :] = 1 / n_classes
c = np.tile(c, (n_classes, 1)).transpose()
hy_x = hy_x / c
return hy_x
if __name__ == '__main__':
# Get the command line arguments.
warnings.simplefilter("ignore")
path = "./Results/Feature selection/"
dataset_name = sys.argv[1]
datasets = ["Arcene", "Colon", "GLI_85", "Leukemia", "Ovarian", "Prostate_GE", "SMK_CAN_187"]
eps = 1e-4
s = 1
n_max = 100
k_max = 20
#---> Loading the dataset
load_dataset = 'load_' + dataset_name
X, y = eval(load_dataset + "(return_X_y=True)")
n, d = X.shape
n_classes = len(np.unique(y))
fit_intercept = True
print('Dataset ' + str(dataset_name) + ' loaded. The dimensions are : ' + str(n) + ', ' + str(d))
use_mrc_cg = True
use_svm_cg = True
use_rfe = True
use_mrmr = True
use_annova = True
# use_
seed = 42
mrc_cg_time_arr = []
svm_cg_time_arr = []
rfe_time_arr = []
mrmr_time_arr = []
annova_time_arr = []
# Errors
lr_mrc_cg_error_arr = []
dt_mrc_cg_error_arr = []
lr_svm_cg_error_arr = []
dt_svm_cg_error_arr = []
lr_rfe_error_arr = []
dt_rfe_error_arr = []
lr_mrmr_error_arr = []
dt_mrmr_error_arr = []
lr_annova_error_arr = []
dt_annova_error_arr = []
# Feature arrays
n_feats_mrc_cg = []
n_feats_svm_cg = []
n_splits = 10
X = StandardScaler().fit_transform(X, y)
cv = StratifiedKFold(n_splits=n_splits, random_state=seed, shuffle=True)
i = 0
# Paired and stratified cross-validation
for train_index, test_index in cv.split(X, y):
print('Cross validation iteration : ', i)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
n_train = X_train.shape[0]
d = X.shape[1]
n_est = n_train
# Feature mapping
phi_ob = BasePhi(n_classes=n_classes,
fit_intercept=fit_intercept,
one_hot=False).fit(X_train, y_train)
#---> MRC_CG (Constraint generation for MRCs)
if use_mrc_cg:
mu, nu, R, I, R_k, mrc_cg_time, _ = mrc_cg(X_train,
y_train,
phi_ob,
s,
n_max,
k_max,
eps)
# Remove the values of mu close to zero
ind_to_remove = np.where(np.isclose(mu[I], 0))[0]
I = list(set(I) - set(np.asarray(I)[ind_to_remove]))
mu[ind_to_remove] = 0
# Map the features to original space
indices = []
for ind in I:
if (ind - 1) >= 0:
indices.append(ind - 1)
# Using features from MRC-CG to perform the classification using LR and DT
lr_classif = LogisticRegression(random_state=seed, penalty='none').fit(X_train[:, indices], y_train)
y_pred = lr_classif.predict(X_test[:, indices])
lr_mrc_cg_error = np.average(y_test != y_pred)
dt_clf = tree.DecisionTreeClassifier(random_state=seed).fit(X_train[:, indices], y_train)
y_pred = dt_clf.predict(X_test[:, indices])
dt_mrc_cg_error = np.average(y_test != y_pred)
# Add the data to the array to be averaged
mrc_cg_time_arr.append(mrc_cg_time)
lr_mrc_cg_error_arr.append(lr_mrc_cg_error)
dt_mrc_cg_error_arr.append(dt_mrc_cg_error)
n_feats_mrc_cg.append(len(I))
#---> SVM CG (Constraint generation for L1 SVM)
if use_svm_cg:
initTime = time.time()
y_train1 = y_train.copy()
y_test1 = y_test.copy()
y_train1[y_train1 == 0] = -1
y_train1[y_train1 == 1] = 1
y_test1[y_test1 == 0] = -1
y_test1[y_test1 == 1] = 1
lam_max = np.max(np.sum( np.abs(X_train), axis=0))
lam = 0.01*lam_max
obj, time_total, time_CG, beta, beta0, support = use_FOM_CG(X_train, y_train1, lam=lam, tau_max=0.1, tol=eps)
# Using features from SVM-CG to perform the classification using LR and DT
lr_classif = LogisticRegression(random_state=seed, penalty='none').fit(X_train[:, support], y_train)
y_pred = lr_classif.predict(X_test[:, support])
lr_svm_cg_error = np.average(y_test != y_pred)
dt_clf = tree.DecisionTreeClassifier(random_state=seed).fit(X_train[:, support], y_train)
y_pred = dt_clf.predict(X_test[:, support])
dt_svm_cg_error = np.average(y_test != y_pred)
svm_cg_time = time.time() - initTime
# Add the data to the array to be averaged
svm_cg_time_arr.append(svm_cg_time)
lr_svm_cg_error_arr.append(lr_svm_cg_error)
dt_svm_cg_error_arr.append(dt_svm_cg_error)
n_feats_svm_cg.append(len(support.tolist()))
#---> SVM RFE
if use_rfe:
initTime = time.time()
estimator = LinearSVC(penalty='l1', random_state=seed, dual=False)
selector = RFE(estimator, n_features_to_select=len(I))
selector = selector.fit(X_train, y_train)
support = selector.get_support(indices=True).tolist()
rfe_time = time.time() - initTime
lr_est = LogisticRegression(random_state=seed, penalty='none').fit(X_train[:, support], y_train)
y_pred = lr_est.predict(X_test[:, support])
lr_rfe_error = np.average(y_test != y_pred)
dt_clf = tree.DecisionTreeClassifier(random_state=seed).fit(X_train[:, support], y_train)
y_pred = dt_clf.predict(X_test[:, support])
dt_rfe_error = np.average(y_test != y_pred)
# Add the data to the array to be averaged
rfe_time_arr.append(rfe_time)
lr_rfe_error_arr.append(lr_rfe_error)
dt_rfe_error_arr.append(dt_rfe_error)
#---> Minimum redundancy maximum relevance
if use_mrmr:
print('Using minimum redundancy maximum relevance')
initTime = time.time()
y_train1 = y_train.copy()
y_test1 = y_test.copy()
y_train1[y_train1 == 0] = -1
y_train1[y_train1 == 1] = 1
y_test1[y_test1 == 0] = -1
y_test1[y_test1 == 1] = 1
pd_x_train = pd.DataFrame(X_train)
pd_y_train = pd.DataFrame(y_train1)
selected_feats = mrmr_classif(X=pd_x_train, y=pd_y_train, K=len(I))
mrmr_time = time.time() - initTime
lr_est = LogisticRegression(random_state=seed, penalty='none').fit(X_train[:, selected_feats], y_train)
y_pred = lr_est.predict(X_test[:, selected_feats])
lr_mrmr_error = np.average(y_test != y_pred)
dt_clf = tree.DecisionTreeClassifier(random_state=seed).fit(X_train[:, selected_feats], y_train)
y_pred = dt_clf.predict(X_test[:, selected_feats])
dt_mrmr_error = np.average(y_test != y_pred)
# Add the data to the array to be averaged
mrmr_time_arr.append(mrmr_time)
lr_mrmr_error_arr.append(lr_mrmr_error)
dt_mrmr_error_arr.append(dt_mrmr_error)
if use_annova:
print('Using ANNOVA ')
initTime = time.time()
y_train1 = y_train.copy()
y_test1 = y_test.copy()
y_train1[y_train1 == 0] = -1
y_train1[y_train1 == 1] = 1
y_test1[y_test1 == 0] = -1
y_test1[y_test1 == 1] = 1
f_score, p_score = f_classif(X=X_train, y=y_train1)
selected_feats = (np.argsort(f_score)[::-1])[:len(I)]
annova_time = time.time() - initTime
lr_est = LogisticRegression(random_state=seed, penalty='none').fit(X_train[:, selected_feats], y_train)
y_pred = lr_est.predict(X_test[:, selected_feats])
lr_annova_error = np.average(y_test != y_pred)
dt_clf = tree.DecisionTreeClassifier(random_state=seed).fit(X_train[:, selected_feats], y_train)
y_pred = dt_clf.predict(X_test[:, selected_feats])
dt_annova_error = np.average(y_test != y_pred)
# Add the data to the array to be averaged
annova_time_arr.append(annova_time)
lr_annova_error_arr.append(lr_annova_error)
dt_annova_error_arr.append(dt_annova_error)
i = i + 1
# Print and save the data
if use_mrc_cg:
avg_time_mrc_cg = np.asarray([np.average(mrc_cg_time_arr),
np.std(mrc_cg_time_arr)])
avg_error_lr = np.asarray([np.average(lr_mrc_cg_error_arr),
np.std(lr_mrc_cg_error_arr)])
avg_error_dt = np.asarray([np.average(dt_mrc_cg_error_arr),
np.std(dt_mrc_cg_error_arr)])
avg_feats = np.asarray([np.average(n_feats_mrc_cg),
np.std(n_feats_mrc_cg)])
# Time
print('Average time taken by MRC-CG : \t' + str(avg_time_mrc_cg[0]) \
+ ' +/- ' + str(avg_time_mrc_cg[1]))
np.savetxt(path + str(dataset_name) + '/mrc_cg_time.csv', avg_time_mrc_cg, delimiter=",", fmt='%s')
# LR error
print('Average error for LR classifier using MRC-CG features : \t' + str(avg_error_lr[0]) \
+ ' +/- ' + str(avg_error_lr[1]))
np.savetxt(path + str(dataset_name) + '/lr_mrc_cg_error.csv', avg_error_lr, delimiter=",", fmt='%s')
# DT error
print('Average error for DT classifier using MRC-CG features : \t' + str(avg_error_dt[0]) \
+ ' +/- ' + str(avg_error_dt[1]))
np.savetxt(path + str(dataset_name) + '/dt_mrc_cg_error.csv', avg_error_dt, delimiter=",", fmt='%s')
# Average number of features selected
print('Average number of features selcted using MRC-CG : \t' + str(avg_feats[0]) \
+ ' +/- ' + str(avg_feats[1]))
np.savetxt(path + str(dataset_name) + '/mrc_cg_n_feats.csv', avg_feats, delimiter=",", fmt='%s')
if use_svm_cg:
print('\n\n')
avg_time_svm_cg = np.asarray([np.average(svm_cg_time_arr),
np.std(svm_cg_time_arr)])
avg_error_lr = np.asarray([np.average(lr_svm_cg_error_arr),
np.std(lr_svm_cg_error_arr)])
avg_error_dt = np.asarray([np.average(dt_svm_cg_error_arr),
np.std(dt_svm_cg_error_arr)])
avg_feats = np.asarray([np.average(n_feats_svm_cg),
np.std(n_feats_svm_cg)])
# Time
print('Average time taken by SVM-CG : \t' + str(avg_time_mrc_cg[0]) \
+ ' +/- ' + str(avg_time_mrc_cg[1]))
np.savetxt(path + str(dataset_name) + '/svm_cg_time.csv', avg_time_svm_cg, delimiter=",", fmt='%s')
# LR error
print('Average error for LR classifier using SVM-CG features : \t' + str(avg_error_lr[0]) \
+ ' +/- ' + str(avg_error_lr[1]))
np.savetxt(path + str(dataset_name) + '/lr_svm_cg_error.csv', avg_error_lr, delimiter=",", fmt='%s')
# DT error
print('Average error for DT classifier using SVM-CG features : \t' + str(avg_error_dt[0]) \
+ ' +/- ' + str(avg_error_dt[1]))
np.savetxt(path + str(dataset_name) + '/dt_svm_cg_error.csv', avg_error_dt, delimiter=",", fmt='%s')
# Average number of features selected
print('Average number of features selcted using SVM-CG : \t' + str(avg_feats[0]) \
+ ' +/- ' + str(avg_feats[1]))
np.savetxt(path + str(dataset_name) + '/svm_cg_n_feats.csv', avg_feats, delimiter=",", fmt='%s')
if use_rfe:
avg_time_rfe = np.asarray([np.average(rfe_time_arr),
np.std(rfe_time_arr)])
avg_error_lr = np.asarray([np.average(lr_rfe_error_arr),
np.std(lr_rfe_error_arr)])
avg_error_dt = np.asarray([np.average(dt_rfe_error_arr),
np.std(dt_rfe_error_arr)])
# Time
print('Average time taken by RFE : \t' + str(avg_time_rfe[0]) \
+ ' +/- ' + str(avg_time_rfe[1]))
np.savetxt(path + str(dataset_name) + '/rfe_time.csv', avg_time_rfe, delimiter=",", fmt='%s')
# LR error
print('Average error for LR classifier using RFE features : \t' + str(avg_error_lr[0]) \
+ ' +/- ' + str(avg_error_lr[1]))
np.savetxt(path + str(dataset_name) + '/lr_rfe_error.csv', avg_error_lr, delimiter=",", fmt='%s')
# DT error
print('Average error for DT classifier using RFE features : \t' + str(avg_error_dt[0]) \
+ ' +/- ' + str(avg_error_dt[1]))
np.savetxt(path + str(dataset_name) + '/dt_rfe_error.csv', avg_error_dt, delimiter=",", fmt='%s')
print('Note that the number of features selected by RFE is equal to the number of features selected by MRC-CG')
if use_mrmr:
avg_time_mrmr = np.asarray([np.average(mrmr_time_arr),
np.std(mrmr_time_arr)])
avg_error_lr = np.asarray([np.average(lr_mrmr_error_arr),
np.std(lr_mrmr_error_arr)])
avg_error_dt = np.asarray([np.average(dt_mrmr_error_arr),
np.std(dt_mrmr_error_arr)])
# Time
print('Average time taken by MRMR : \t' + str(avg_time_mrmr[0]) \
+ ' +/- ' + str(avg_time_mrmr[1]))
np.savetxt(path + str(dataset_name) + '/mrmr_time.csv', avg_time_mrmr, delimiter=",", fmt='%s')
# LR error
print('Average error for LR classifier using MRMR features : \t' + str(avg_error_lr[0]) \
+ ' +/- ' + str(avg_error_lr[1]))
np.savetxt(path + str(dataset_name) + '/lr_mrmr_error.csv', avg_error_lr, delimiter=",", fmt='%s')
# DT error
print('Average error for DT classifier using MRMR features : \t' + str(avg_error_dt[0]) \
+ ' +/- ' + str(avg_error_dt[1]))
np.savetxt(path + str(dataset_name) + '/dt_mrmr_error.csv', avg_error_dt, delimiter=",", fmt='%s')
print('Note that the number of features selected by MRMR is equal to the number of features selected by MRC-CG')
if use_annova:
avg_time_annova = np.asarray([np.average(annova_time_arr),
np.std(annova_time_arr)])
avg_error_lr = np.asarray([np.average(lr_annova_error_arr),
np.std(lr_annova_error_arr)])
avg_error_dt = np.asarray([np.average(dt_annova_error_arr),
np.std(dt_annova_error_arr)])
# Time
print('Average time taken by ANNOVA : \t' + str(avg_time_annova[0]) \
+ ' +/- ' + str(avg_time_annova[1]))
np.savetxt(path + str(dataset_name) + '/annova_time.csv', avg_time_annova, delimiter=",", fmt='%s')
# LR error
print('Average error for LR classifier using ANNOVA features : \t' + str(avg_error_lr[0]) \
+ ' +/- ' + str(avg_error_lr[1]))
np.savetxt(path + str(dataset_name) + '/lr_annova_error.csv', avg_error_lr, delimiter=",", fmt='%s')
# DT error
print('Average error for DT classifier using ANNOVA features : \t' + str(avg_error_dt[0]) \
+ ' +/- ' + str(avg_error_dt[1]))
np.savetxt(path + str(dataset_name) + '/dt_annova_error.csv', avg_error_dt, delimiter=",", fmt='%s')
print('Note that the number of features selected by ANNOVA is equal to the number of features selected by MRC-CG')
print('Data saved successfully in ', path + str(dataset_name))