|
a |
|
b/python/oversampling.py |
|
|
1 |
#!/usr/bin/env python |
|
|
2 |
|
|
|
3 |
""" |
|
|
4 |
train_SVM.py |
|
|
5 |
|
|
|
6 |
VARPA, University of Coruna |
|
|
7 |
Mondejar Guerra, Victor M. |
|
|
8 |
15 Dec 2017 |
|
|
9 |
""" |
|
|
10 |
|
|
|
11 |
import os |
|
|
12 |
import csv |
|
|
13 |
import gc |
|
|
14 |
import cPickle as pickle |
|
|
15 |
import time |
|
|
16 |
from imblearn.over_sampling import SMOTE, ADASYN |
|
|
17 |
from imblearn.combine import SMOTEENN, SMOTETomek |
|
|
18 |
import collections |
|
|
19 |
from sklearn import svm |
|
|
20 |
import numpy as np |
|
|
21 |
|
|
|
22 |
cpu_threads = 7 |
|
|
23 |
|
|
|
24 |
# http://contrib.scikit-learn.org/imbalanced-learn/stable/auto_examples/combine/plot_comparison_combine.html#sphx-glr-auto-examples-combine-plot-comparison-combine-py |
|
|
25 |
|
|
|
26 |
# Perform the oversampling method over the descriptor data |
|
|
27 |
def perform_oversampling(oversamp_method, db_path, oversamp_features_name, tr_features, tr_labels): |
|
|
28 |
start = time.time() |
|
|
29 |
|
|
|
30 |
oversamp_features_pickle_name = db_path + oversamp_features_name + '_' + oversamp_method + '.p' |
|
|
31 |
print(oversamp_features_pickle_name) |
|
|
32 |
|
|
|
33 |
if True: |
|
|
34 |
print("Oversampling method:\t" + oversamp_method + " ...") |
|
|
35 |
# 1 SMOTE |
|
|
36 |
if oversamp_method == 'SMOTE': |
|
|
37 |
#kind={'borderline1', 'borderline2', 'svm'} |
|
|
38 |
svm_model = svm.SVC(C=0.001, kernel='rbf', degree=3, gamma='auto', decision_function_shape='ovo') |
|
|
39 |
oversamp = SMOTE(ratio='auto', random_state=None, k_neighbors=5, m_neighbors=10, out_step=0.5, kind='svm', svm_estimator=svm_model, n_jobs=1) |
|
|
40 |
|
|
|
41 |
# PROBAR SMOTE CON OTRO KIND |
|
|
42 |
|
|
|
43 |
elif oversamp_method == 'SMOTE_regular_min': |
|
|
44 |
oversamp = SMOTE(ratio='minority', random_state=None, k_neighbors=5, m_neighbors=10, out_step=0.5, kind='regular', svm_estimator=None, n_jobs=1) |
|
|
45 |
|
|
|
46 |
elif oversamp_method == 'SMOTE_regular': |
|
|
47 |
oversamp = SMOTE(ratio='auto', random_state=None, k_neighbors=5, m_neighbors=10, out_step=0.5, kind='regular', svm_estimator=None, n_jobs=1) |
|
|
48 |
|
|
|
49 |
elif oversamp_method == 'SMOTE_border': |
|
|
50 |
oversamp = SMOTE(ratio='auto', random_state=None, k_neighbors=5, m_neighbors=10, out_step=0.5, kind='borderline1', svm_estimator=None, n_jobs=1) |
|
|
51 |
|
|
|
52 |
# 2 SMOTEENN |
|
|
53 |
elif oversamp_method == 'SMOTEENN': |
|
|
54 |
oversamp = SMOTEENN() |
|
|
55 |
|
|
|
56 |
# 3 SMOTE TOMEK |
|
|
57 |
# NOTE: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.65.3904&rep=rep1&type=pdf |
|
|
58 |
elif oversamp_method == 'SMOTETomek': |
|
|
59 |
oversamp = SMOTETomek() |
|
|
60 |
|
|
|
61 |
# 4 ADASYN |
|
|
62 |
elif oversamp_method == 'ADASYN': |
|
|
63 |
oversamp = ADASYN(ratio='auto', random_state=None, k=None, n_neighbors=5, n_jobs=cpu_threads) |
|
|
64 |
|
|
|
65 |
tr_features_balanced, tr_labels_balanced = oversamp.fit_sample(tr_features, tr_labels) |
|
|
66 |
# TODO Write data oversampled! |
|
|
67 |
print("Writing oversampled data at: " + oversamp_features_pickle_name + " ...") |
|
|
68 |
np.savetxt('mit_db/' + oversamp_features_name + '_DS1_labels.csv', tr_labels_balanced.astype(int), '%.0f') |
|
|
69 |
f = open(oversamp_features_pickle_name, 'wb') |
|
|
70 |
pickle.dump(tr_features_balanced, f, 2) |
|
|
71 |
f.close |
|
|
72 |
|
|
|
73 |
end = time.time() |
|
|
74 |
|
|
|
75 |
count = collections.Counter(tr_labels_balanced) |
|
|
76 |
print("Oversampling balance") |
|
|
77 |
print(count) |
|
|
78 |
print("Time required: " + str(format(end - start, '.2f')) + " sec" ) |
|
|
79 |
|
|
|
80 |
return tr_features_balanced, tr_labels_balanced |