-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolo_training.py
251 lines (219 loc) · 10.9 KB
/
solo_training.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
import numpy as np
import scipy.io as sio
from sklearn import preprocessing, model_selection, neighbors, svm, metrics
from sklearn.neural_network import MLPClassifier as nn
import pandas as pd
from os import listdir
from os.path import join
from sklearn.linear_model import LogisticRegression
import multiprocessing
def past_features(X):
X_1 = X[0:-2, :]
X_2 = X[1:-1, :]
X = X[2:, :]
X = np.concatenate((X_1, X_2, X), axis=1)
return X
def train_svm():
svm_parameters = np.array([[0], [0], [0]])
mypath = '/Users/reza/Desktop/EEG/data'
result_path = '/Users/reza/Desktop/EEG/results'
files = [f for f in listdir(mypath)]
for f in files:
if f == '.DS_Store':
continue
with open(join(result_path, f + '_results.txt'), 'a') as result_file:
print('=================================================================================')
print(f)
data = sio.loadmat(join(mypath, f))['data']
data = np.array(data)
print(data.shape)
np.random.shuffle(data)
temp = data.shape[1]-1
X = np.array(data[:, 0:temp])
X = (X - X.mean(axis=0)) / (X.max(axis=0) - X.min(axis=0))
y = np.array(data[:, temp])
print('ys:' + str(np.sum(y)))
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2)
# X_test = X_train
# y_test = y_train
my_score = 0
sensitivity = 0
specificity = 0
best_c = best_g = best_w = 0
best_sens = best_spec = best_prec = 0
count = 0
test_range = np.logspace(start=0.0, stop=8.0, num=15, base=2, dtype=int)
w_range = np.logspace(start=1.0, stop=7, base=2, num=10, dtype=int)
test_range = np.concatenate(([0.01, 0.06, 0.1, 0.3, 0.8], test_range[1:]), axis=0)
print(test_range)
# test_range = [0.8, 1.5]
# w_range = [8]
for c in [20,30]: # 0.003, 0.01, 0.06, 0.1, 0.5, 0.8, 1, 3, 9, 20, 60
for g in [20,30]:
for w in [10]:
print('\n\n******************')
result_file.write('\n*********************')
count = count + 1
print(count)
result_file.write('\nIteration: {}'.format(count))
clf = svm.SVC(C=c, verbose=3, max_iter=10000, kernel='rbf', class_weight={1: w, 0: 1}, gamma=g)
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
test = clf.predict(X_test)
tp = tn = fp = fn = 0
for i in range(len(y_test)):
if y_test[i] == 1:
if test[i] == 1:
tp = tp + 1
else:
fn = fn + 1
else:
if test[i] == 1:
fp = fp + 1
else:
tn = tn + 1
print('\nC: {}, Gamma: {}, Weight: {}'.format(c, g, w))
result_file.write('\nC: {}, Gamma: {}, Weight: {}'.format(c, g, w))
print('tp: {}'.format(tp))
result_file.write('\ntp: {}'.format(tp))
print('tn: {}'.format(tn))
result_file.write('\ntn: {}'.format(tn))
print('fp: {}'.format(fp))
result_file.write('\nfp: {}'.format(fp))
print('fn: {}'.format(fn))
result_file.write('\nfn: {}'.format(fn))
sensitivity = tp / (tp + fn)
if (tp + fp) != 0:
precision = tp / (tp + fp)
else:
precision = 0
specificity = tn / (tn + fp)
print('sensitivity: {}'.format(sensitivity))
result_file.write('\nsensitivity: {}'.format(sensitivity))
print('precision: {}'.format(precision))
result_file.write('\nprecision: {}'.format(precision))
print('specificity: {}'.format(specificity))
result_file.write('\nspecificity: {}'.format(specificity))
f1 = metrics.f1_score(y_test, test, average='macro')
print('f1 macro score: {}'.format(f1))
result_file.write('\nf1 macro score: {}'.format(f1))
#
# print('accuracy')
# print(accuracy)
# my_metric = 0
# if fp+tp != 0:
# my_metric = tp / (fp + tp)
parameter_f1 = 0
if (sensitivity + precision) != 0: parameter_f1 = (2 * sensitivity * precision) / (sensitivity + precision)
if (sensitivity + precision) != 0 and (
(2 * sensitivity * precision) / (sensitivity + precision)) > my_score:
my_score = (2 * sensitivity * precision) / (sensitivity + precision)
best_c = c
best_g = g
best_w = w
best_sens = sensitivity
best_spec = specificity
best_prec = precision
print('best Score so far: {}'.format(my_score))
result_file.write('\nbest Score so far: {}'.format(my_score))
svm_parameters = np.hstack([svm_parameters, [[c],[g],[parameter_f1]]])
print('F1 Score: {}'.format(my_score))
result_file.write(('\nF1 Score: {}'.format(my_score)))
print('\n\n******************')
result_file.write('\n*****************************\n')
print('\n Final Results for {}:'.format(f))
result_file.write('\nFinal Results for {}:'.format(f))
print('best score: {}'.format(my_score))
result_file.write('\nbest score: {}'.format(my_score))
print('C: {}, Gamma: {}, Weight: {}'.format(best_c, best_g, best_w))
result_file.write('\nC: {}, Gamma: {}, Weight: {}'.format(best_c, best_g, best_w))
print('sensitivity: {}'.format(best_sens))
result_file.write('\nsensitivity: {}'.format(best_sens))
print('precision: {}'.format(best_prec))
result_file.write('\nprecision: {}'.format(best_prec))
print('specificity: {}'.format(best_spec))
result_file.write('\nspecificity: {}'.format(best_spec))
print('=================================================================================\n\n\n')
svm_parameters.tofile('parameters.dat')
def train_nn():
mypath = '/Users/reza/Desktop/EEG/Code to Give/SVM_TRAINING/3seconds_7.0Hz/'
result_path = '/Users/reza/Desktop/EEG/rbf_results/shuffle/'
files = [f for f in listdir(mypath)]
for f in files:
if f == '.DS_Store':
continue
with open(join(result_path, f + '_results.txt'), 'a') as result_file:
print('=================================================================================')
print(f)
data = sio.loadmat(join(mypath, f))['data']
data = np.array(data)
np.random.shuffle(data)
X = np.array(data[:, 0:40])
kernel = np.random.uniform(low=-1, high=1, size=(40, 500))
X = X @ kernel
X = (X - X.mean(axis=0)) / (X.max(axis=0) - X.min(axis=0))
y = np.array(data[:, 40])
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2)
# X_test = X_train
# y_test = y_train
test_range = np.logspace(start=-20, stop=7, num=15, base=2, dtype=float)
my_score = 0
sensitivity = 0
specificity = 0
best_a = 0
for a in test_range:
print('\n\n******************')
result_file.write('\n*********************')
print('alpha: {}'.format(a))
clf = nn(solver='lbfgs', alpha=a, hidden_layer_sizes=(10,10,10,4))
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
test = clf.predict(X_test)
tp = tn = fp = fn = 0
for i in range(len(y_test)):
if y_test[i] == 1:
if test[i] == 1:
tp = tp + 1
else:
fn = fn + 1
else:
if test[i] == 1:
fp = fp + 1
else:
tn = tn + 1
print('tp: {}'.format(tp))
result_file.write('\ntp: {}'.format(tp))
print('tn: {}'.format(tn))
result_file.write('\ntn: {}'.format(tn))
print('fp: {}'.format(fp))
result_file.write('\nfp: {}'.format(fp))
print('fn: {}'.format(fn))
result_file.write('\nfn: {}'.format(fn))
sensitivity = tp / (tp + fn)
if (tp + fp) != 0:
precision = tp / (tp + fp)
else:
precision = 0
specificity = tn / (tn + fp)
print('sensitivity: {}'.format(sensitivity))
result_file.write('\nsensitivity: {}'.format(sensitivity))
print('precision: {}'.format(precision))
result_file.write('\nprecision: {}'.format(precision))
print('specificity: {}'.format(specificity))
result_file.write('\nspecificity: {}'.format(specificity))
if (sensitivity + precision) != 0 and (
(2 * sensitivity * precision) / (sensitivity + precision)) > my_score:
my_score = (2 * sensitivity * precision) / (sensitivity + precision)
best_a = a
best_sens = sensitivity
best_spec = specificity
best_prec = precision
print('best Score so far: {}'.format(my_score))
result_file.write('\nbest Score so far: {}'.format(my_score))
#
# print('accuracy')
# print(accuracy)
# my_metric = 0
# if fp+tp != 0:
# my_metric = tp / (fp + tp)
train_svm()