-
Notifications
You must be signed in to change notification settings - Fork 1
/
RFE.py
411 lines (366 loc) · 12 KB
/
RFE.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
import numpy as np
import pickle as pkl
import pandas as pd
import json
import os
import argparse
from time import time
from enum import Enum
from statistics import mean
from scipy.stats import rankdata
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score
from sklearn.feature_selection import RFE
from rac import RAClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.utils.multiclass import unique_labels
from sklearn.metrics import auc
from utils import Scorer
from utils import compute_scores, compute_roc_auc
# ------------------- Classifiers available for comparison ---------------------
class Clfs(Enum):
RAC = 'RAC'
SVM = 'SVM'
RF = 'RF'
XGB = 'XGB'
# ----------------------------- Scores evaluated -------------------------------
scores = {
'accuracy': Scorer(accuracy_score),
'f1 micro': Scorer(f1_score, average='micro'),
'f1 macro': Scorer(f1_score, average='macro'),
'f1 weighted': Scorer(f1_score, average='weighted')
}
# -------------------------- Cmd line argument parsing -------------------------
parser = argparse.ArgumentParser(description='Recursive Feature Elimination')
parser.add_argument(
'--dataset',
action='store',
type=str,
required=True,
help="Path to the dataset."
)
parser.add_argument(
'--rac-params',
action='store',
type=str,
required=True,
help="Path to the .json file with RAC parameters."
)
parser.add_argument(
'--svm-params',
action='store',
type=str,
required=True,
help="Path to the .json file with SVM parameters (kernel must be 'linear')."
)
parser.add_argument(
'--rf-params',
action='store',
type=str,
required=True,
help="Path to the .json file with RF parameters."
)
parser.add_argument(
'--xgb-params',
action='store',
type=str,
required=True,
help="Path to the .json file with XGB parameters."
)
parser.add_argument(
'--pos-lbl',
action='store',
type=str,
default=None,
help="Label of the 'positive' class in binary classification."
)
parser.add_argument(
'--output-path',
action='store',
type=str,
required=True,
help="Path where to save results."
)
parser.add_argument(
'--n-splits',
action='store',
default=5,
type=int,
help="Number of folds to use."
)
parser.add_argument(
'--random-state',
action='store',
type=int,
default=0,
help="Seed to get reproducible results."
)
parser.add_argument(
'--standardize',
action=argparse.BooleanOptionalAction,
default=False,
help="Whether to standardize features."
)
parser.add_argument(
'--n-features-to-select',
action='store',
type=int,
default=20,
help="RFE parameter."
)
parser.add_argument(
'--step',
action='store',
type=float,
default=0.5,
help="RFE parameter."
)
parser.add_argument(
'--verbose',
action=argparse.BooleanOptionalAction,
default=False,
help="Whether to print verbose output."
)
args = parser.parse_args()
dataset = args.dataset
rac_params_path = args.rac_params
svm_params_path = args.svm_params
rf_params_path = args.rf_params
xgb_params_path = args.xgb_params
pos_lbl = args.pos_lbl
output_path = args.output_path
n_splits = args.n_splits
random_state = args.random_state
standardize = args.standardize
n_features_to_select = args.n_features_to_select
step = args.step
verbose = args.verbose
# ------------------------- Cmd line argument validation -----------------------
with open(dataset, 'rb') as fp :
dataset_dict = pkl.load(fp)
X = dataset_dict['X']
y = dataset_dict['y']
features_names = np.array(dataset_dict['features_ids'])
labels = unique_labels(y)
binary = len(labels) <= 2
if binary and pos_lbl and labels[1] != pos_lbl:
if labels[0] != pos_lbl:
raise ValueError("Invalid poslbl (allowed values are " +
str(labels) + ")")
else:
# swap labels
tmp = labels[0]
labels[0] = labels[1]
labels[1] = tmp
with open(rac_params_path) as rac_params_fp:
rac_params = json.load(rac_params_fp)
with open(svm_params_path) as svm_params_fp:
svm_params = json.load(svm_params_fp)
with open(rf_params_path) as rf_params_fp:
rf_params = json.load(rf_params_fp)
with open(xgb_params_path) as xgb_params_fp:
xgb_params = json.load(xgb_params_fp)
# Dict to store classifiers parameters
clfs_params = {}
clfs_params['RAC']=[]
clfs_params['SVM']=[]
clfs_params['RF']=[]
clfs_params['XGB']=[]
for i in range(n_splits):
clfs_params['RAC'].append(rac_params)
clfs_params['SVM'].append(svm_params)
clfs_params['RF'].append(rf_params)
clfs_params['XGB'].append(xgb_params)
print(clfs_params['RAC'])
print(clfs_params['RF'])
print(clfs_params['SVM'])
print(clfs_params['XGB'])
if not os.path.isdir(output_path):
raise ValueError(
"Invalid resfoder ('" + output_path + "' is not an existing directory)."
)
if not output_path.endswith('/'):
raise ValueError("Invalid resfoder (must end with /.)")
# Dict to store classifiers classes
clfs_classes = {}
clfs_classes[Clfs.RAC.name] = RAClassifier
clfs_classes[Clfs.SVM.name] = SVC
clfs_classes[Clfs.RF.name] = RandomForestClassifier
clfs_classes[Clfs.XGB.name] = XGBClassifier
if n_splits < 2:
raise ValueError("Invalid nsplits (must be an int >= 2).")
multiclass = "ovr"
scaler = False
if standardize:
scaler = StandardScaler()
# ------------------------------- Cross validation ----------------------------
# Dict to store the concatenation of predictions, the values of the scores
# and the true targets from cross validation
predictions = {}
for clf_name in clfs_classes:
predictions[clf_name] = {}
# Dict to store cross validation results
results = {}
for i in range(n_splits):
results['split%d params' % i] = []
for i in range(n_splits):
results['split%d fit times' % i] = []
for i in range(n_splits):
results['split%d score times' % i] = []
for score in scores:
for i in range(n_splits):
results['split%d %s' % (i, score)] = []
results['mean '+score] = []
results['concat '+score] = []
for i in range(n_splits):
results['split%d auroc' % i] = []
results['mean auroc'] = []
results['concat auroc'] = []
results['concat sklearn auroc'] = []
# Iterate on classifiers
for clf_name, base_clf in clfs_classes.items():
if verbose: print(clf_name+" classifier")
# Splitting strategy to use
cv = StratifiedKFold(
n_splits=n_splits,
shuffle=True,
random_state=random_state)
# Concatenated predictions, true targets and scores from test folds
y_pred_concat = np.array([])
y_true_concat = np.array([])
y_score_concat = None
# Dict to store scores from test folds
cv_scores = {}
for score_name in scores:
cv_scores[score_name] = []
cv_scores['auroc'] = []
# Dict to store selected feature with this classifier
selected_features = {}
# Cross validation
for split, (train_index , test_index) in enumerate(cv.split(X, y)):
if verbose: print('[CV %d/%d]' % (split+1, n_splits))
X_train, X_val = X[train_index, :], X[test_index, :]
y_train, y_val = y[train_index] , y[test_index]
# Data scaling
if standardize:
X_train = scaler.fit_transform(X_train)
X_val = scaler.transform(X_val)
# Fit
clf_params = clfs_params[clf_name][split]
if base_clf==SVC:
print(clf_params)
clf_params['probability'] = True
selector = RFE(
base_clf(**clf_params),
n_features_to_select=n_features_to_select,
step=step
)
fit_start = time()
selector = selector.fit(X_train, y_train)
fit_time = time()-fit_start
# Store selected features
for f in features_names[selector.support_]:
if f not in selected_features:
selected_features[f] = [False for i in range(n_splits)]
selected_features[f][split] = True
results['split%d params' % split].append(clf_params)
results['split%d fit times' % split].append(fit_time)
# Test the model on the validation set
score_start = time()
y_pred = selector.predict(X_val)
results['split%d score times' % split].append(time()-score_start)
# Get probabilities associated to predictions
y_score = selector.predict_proba(X_val)
# Compute and store AUROC score on this test fold
if binary:
auroc_s = roc_auc_score(
y_val,
y_score[:, 1]
)
else:
auroc_s = roc_auc_score(
y_val,
y_score,
labels=clf.classes_,
average="weighted",
multi_class=multiclass
)
results['split%d auroc' % split].append(auroc_s)
cv_scores['auroc'].append(auroc_s)
# Concatenate predictions made on this fold
y_pred_concat = np.concatenate([y_pred_concat, y_pred])
y_true_concat = np.concatenate([y_true_concat, y_val])
y_score_concat = np.concatenate([y_score_concat, y_score]) \
if y_score_concat is not None else y_score
# Compute and store scores on this fold
test_scores = compute_scores(
scores,
y_pred=y_pred,
y_true=y_val,
y_score=y_score
)
for score_name, value in test_scores.items():
results['split%d %s' % (split, score_name)].append(value)
cv_scores[score_name].append(value)
if verbose: print('%s=%f ' % (score_name, value), end='')
if verbose: print()
# Save selected features into a csv file
feature_count = []
for f, splits in selected_features.items():
feature_count.append(splits.count(True))
df = pd.DataFrame.from_dict(
selected_features,
orient='index',
columns=['split%d'%i for i in range(n_splits)]
)
df['count']= feature_count
df.to_csv(output_path+'%s_features.csv'%clf_name)
# Compute and store mean scores on test folds
for score_name, value in cv_scores.items():
results['mean '+score_name].append(mean(value))
# Compute and store scores on the concatenation of the predictions on test folds
concat_scores = compute_scores(
scores,
y_pred=y_pred_concat,
y_true=y_true_concat,
y_score=y_score_concat
)
for score_name, value in concat_scores.items():
results['concat '+score_name].append(value)
# Save predictions, true targets and scores
predictions[clf_name]['preds'] = y_pred_concat
predictions[clf_name]['true'] = y_true_concat
predictions[clf_name]['probs'] = y_score_concat
# Compute auroc
fpr, tpr, auroc = compute_roc_auc(
y_true=y_true_concat,
y_score=y_score_concat,
weighted=True,
labels=labels
)
results['concat auroc'].append(auroc)
results['concat sklearn auroc'].append(auc(fpr, tpr))
# Rank models
aggr_rank = np.zeros((len(clfs_classes)))
scores['auroc'] = Scorer(None)
for score_name, scorer in scores.items():
mean_score = np.array(results['mean '+score_name])
if scorer.greater_better: mean_score = 1-mean_score
rank_mean = rankdata(mean_score, method='min').tolist()
results['rank mean '+score_name] = rank_mean
aggr_rank += np.array(rank_mean)
concat_score = np.array(results['concat '+score_name])
if scorer.greater_better: concat_score = 1-concat_score
rank_concat = rankdata(concat_score, method='min').tolist()
results['rank concat '+score_name] = rank_concat
aggr_rank += np.array(rank_concat)
results['aggregated rank'] = rankdata(aggr_rank, method='min').tolist()
# Save test results on csv file
df = pd.DataFrame.from_dict(results, orient='index', columns=clfs_classes.keys())
df.to_csv(output_path+'rfe_cv.csv')
# Save predictions, true targets and scores from test folds
with open(output_path + "rfe_predictions.pkl", "wb") as fp:
pkl.dump(predictions, fp)