-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuportVectorMachine.py
237 lines (201 loc) · 9.65 KB
/
SuportVectorMachine.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
#
# COMBIO PROJECT: DATA ANALISIS
# Marta Alcalde & Núria Mercadé
#
# ===========================================================================
# PREDICCTION MODELS - SUPORT VECTOR MACHINE
# ===========================================================================
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix,roc_curve,roc_auc_score
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
#from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
#from sklearn.tree import DecisionTreeClassifier
#from sklearn.neighbors import KNeighborsClassifier
import xlsxwriter
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Set seed
seed = 90000
# Read Excel
file = "C:/Users/Marta/Dropbox/COMBIO/dades.xlsx"
# file = "/Users/nmercade/Desktop/dades.xlsx"
data_dirty = pd.read_excel(file,header = 0, usecols = "A,B,H:AD")
# Delete one video # 120, 221, 447
data = data_dirty.drop([0,1,118,219,342,445])
data = data.loc[:,("Test","% Infarct","Moving_time(%)","vm_nose(cm/s)","vm_butt",\
"vm_tail_end","vm_pawE","vm_pawD","vm_pawe","vm_pawd",\
"d_nose-but")];
mydataTR = {0: data.loc[(data["Test"] == "TR - POST - 24H") | (data["Test"] == "TR - POST - 48H") |\
(data["Test"] == "TR - POST - 72H") | (data["Test"] == "TR - PRE")],\
1: data.loc[(data["Test"] == "TR - POST - 24H") | (data["Test"] == "TR - POST - 48H") |\
(data["Test"] == "TR - POST - 72H")],\
2: data.loc[(data["Test"] == "TR - PRE")], 3: data.loc[(data["Test"] == "TR - POST - 24H")],\
4: data.loc[(data["Test"] == "TR - POST - 48H")], 5: data.loc[(data["Test"] == "TR - POST - 72H")], \
6: data.loc[(data["Test"] == "TR - POST - 48H") | (data["Test"] == "TR - POST - 72H")]};
datTR = {0: 'ALL', 1: 'ALL POST', 2: 'PRE', 3: 'POST 24H', 4: 'POST 48H', 5: 'POST 72H', 6: 'POST 48H & 72H'}
for jj in range(7):
data = mydataTR[jj];
print('The dataset is {}'.format(datTR[jj]))
print(jj)
# Define predictor and response variables
# 1. Response variable:
# 0: Not having an ictus
# 1: Having an ictus
inf = np.array(data["% Infarct"].dropna()); y = [];
for i in range(len(inf)):
if inf[i] > 0:
y.append(1)
else:
y.append(0)
# 2. Independent variables
x = data.loc[:,"Moving_time(%)":"d_nose-but"];
# Creation of two datasets: test and train
Xtrain, Xtest, Ytrain, Ytest = train_test_split(x,y,test_size = 0.25, random_state = seed)
# Visualization of the response variable to prove if it is balanced.
fig, ax = plt.subplots(figsize=(12, 8))
plt.hist(Ytrain)
# Scale the feature to expect normal distribution of the data.
sc = StandardScaler()
XtrainN = sc.fit_transform(Xtrain)
XtestN = sc.transform(Xtest)
# =========================================================================
# SVM - rbf KERNEL
# =========================================================================
model = SVC(kernel = 'rbf', random_state = seed)
model.fit(XtrainN,Ytrain)
#Prediction of our test data
Ypred = model.predict(XtestN)
# KFold method
cv = RepeatedStratifiedKFold(n_splits = 10, n_repeats = 10, random_state = seed) #cv = StratifiedKFold(n_splits = 10)
# Evaluate model
scores = cross_val_score(model, Xtrain, Ytrain, scoring = 'accuracy', cv = cv)
print('Train accuracy: {}'.format(np.mean(scores)*100))
# Confusion matrix for the rbf kernel
cm = confusion_matrix(Ytest,Ypred)
# Test accuracy
acc = (cm[0,0]+cm[1,1])/sum(sum(cm))*100
print('Test accuracy(linear kernel): {}'.format(acc))
# Sensitivity
sensitivity = cm[0,0]/(cm[0,0]+cm[1,0])*100
print('Sensitivity(linear kernel): The {} were correctly identified as not having an ictus'.format(sensitivity))
# Specificity
specificity = cm[1,1]/(cm[1,1]+cm[0,1])*100
print('Specificity(linear kernel): The {} were correctly identified as having an ictus'.format(specificity))
sns.set(style = 'white')
fig, ax = plt.subplots(figsize=(12, 8))
sns.heatmap(np.eye(2), annot = cm, fmt = 'g', annot_kws = {'size': 30},
cmap = sns.color_palette(['lightcoral', 'darkseagreen'], as_cmap=True), cbar=False,
yticklabels=['Negative', 'Positive'], xticklabels=['Negative', 'Positive'], ax=ax)
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
ax.tick_params(labelsize=19, length = 0)
ax.set_xlabel('Predicted Values', size = 20)
ax.set_ylabel('Actual Values', size = 20)
additional_texts = ['(True negative)', '(False positive)', '(False negative)', '(True positive)']
for text_elt, additional_text in zip(ax.texts, additional_texts):
ax.text(*text_elt.get_position(), '\n' + additional_text, color=text_elt.get_color(),
ha='center', va='top', size=24)
plt.tight_layout()
path = '.\\SVM\\CM_{}'.format(jj)
fig.savefig(path)
# =========================================================================
# ACCURACY RATA
# =========================================================================
# Read the identification of the rat
rat = pd.read_excel(file,header = 0, usecols = "A,B,H:AD");
rat = rat.Mouse;
# Select those identifications that are from the test part
ratTest = rat.iloc[Xtest.index];
# Vector that contains each identification of the rat ones.
lrt = list(set(ratTest));
#convert each Series to a DataFrame (it is easier to operate with them)
rt_df = ratTest.to_frame(name = 'Id');
yp_df = pd.DataFrame(Ypred, columns = ['Pred']); yp_df.index = Xtest.index;
yt_df = pd.DataFrame(Ytest, columns = ['Test']); yt_df.index = Xtest.index;
Rpred = []; Rtest = [];
ix1 = 0;
# Loop to gather predictions and identification of the rat
for ix in lrt:
# Prediction
pos = yp_df[ratTest == lrt[ix1]]
ones = (pos.values == 1).sum()
zero = (pos.values == 0).sum()
if ones > zero:
Rpred.append(1)
if ones < zero:
Rpred.append(0)
if ones == zero:
Rpred.append(2)
# Test data
pos = yt_df[ratTest == lrt[ix1]]
ones = (pos.values == 1).sum()
if ones >= 1:
Rtest.append(1)
else:
Rtest.append(0)
ix1 = ix1 + 1
# Accuracy of rat
cm = confusion_matrix(Rtest,Rpred)
# Test accuracy
acc = (cm[0,0]+cm[1,1])/sum(sum(cm))*100
print('Accuracy of rat', acc)
print(' ')
# ===========================================================================
# SVM - POLYNOMIAL KERNEL
# ===========================================================================
# model = SVC(kernel = 'poly', random_state = seed)
# model.fit(XtrainN, Ytrain)
# Ypred1 = model.predict(XtestN)
# # Confusion matrix for the polinomial kernel
# cm = confusion_matrix(Ytest,Ypred1)
# # Test accuracy
# acc = (cm[0,0]+cm[1,1])/sum(sum(cm))*100
# print('Test accuracy(poly kernel): {}'.format(acc))
# # Sensitivity
# sensitivity = cm[0,0]/(cm[0,0]+cm[1,0])*100
# print('Sensitivity(poly kernel): The {} were correctly identified as not having an ictus'.format(sensitivity))
# # Specificity
# specificity = cm[1,1]/(cm[1,1]+cm[0,1])*100
# print('Specificity(poly kernel): The {} were correctly identified as having an ictus'.format(specificity))
# print(' ')
# ===========================================================================
# SVM - RBF KERNEL
# ===========================================================================
# model = SVC(kernel = 'rbf', random_state = seed)
# model.fit(XtrainN, Ytrain)
# Ypred1 = model.predict(XtestN)
# # Confusion matrix for the polinomial kernel
# cm = confusion_matrix(Ytest,Ypred1)
# # Test accuracy
# acc = (cm[0,0]+cm[1,1])/sum(sum(cm))*100
# print('Test accuracy(RBF kernel): {}'.format(acc))
# # Sensitivity
# sensitivity = cm[0,0]/(cm[0,0]+cm[1,0])*100
# print('Sensitivity(RBF kernel): The {} were correctly identified as not having an ictus'.format(sensitivity))
# # Specificity
# specificity = cm[1,1]/(cm[1,1]+cm[0,1])*100
# print('Specificity(RBF kernel): The {} were correctly identified as having an ictus'.format(specificity))
# print(' ')
# ===========================================================================
# SVM - SIGMOID KERNEL
# ===========================================================================
# model = SVC(kernel = 'sigmoid', random_state = seed)
# model.fit(XtrainN, Ytrain)
# Ypred1 = model.predict(XtestN)
# # Confusion matrix for the polinomial kernel
# cm = confusion_matrix(Ytest,Ypred1)
# # Test accuracy
# acc = (cm[0,0]+cm[1,1])/sum(sum(cm))*100
# print('Test accuracy(sigmoid kernel): {}'.format(acc))
# # Sensitivity
# sensitivity = cm[0,0]/(cm[0,0]+cm[1,0])*100
# print('Sensitivity(sigmoid kernel): The {} were correctly identified as not having an ictus'.format(sensitivity))
# # Specificity
# specificity = cm[1,1]/(cm[1,1]+cm[0,1])*100
# print('Specificity(sigmoid kernel): The {} were correctly identified as having an ictus'.format(specificity))