-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvm.py
391 lines (350 loc) · 16.8 KB
/
svm.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
import random
import numpy as np
import math
import string
import csv
import re
import json
import sys
from collections import Counter,defaultdict
import time
from scipy.sparse import csr_matrix
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from numpy import linalg
import cvxopt
sys.path.append('libsvm-3.23/python')
from svmutil import *
def confusion_matrix_draw(star_actual,star_predicted):
cm = confusion_matrix(star_actual,star_predicted)
plt.imshow(cm)
plt.title('Confusion matrix')
plt.colorbar()
plt.set_cmap('Blues')
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
def LinearKernel(x1, x2,sigma=None):
return np.dot(x1, x2)
def GaussianKernel(x, y):
return np.exp(-linalg.norm(x-y)**2 * 0.05)
def fitgaussian(S1,rowY):
m,n = S1.shape
b=0
aa = time.time()
S11 = np.asarray(S1)
print("time to conv:",time.time()- aa)
KM = np.array([[GaussianKernel(i,j) for j in S11] for i in S11])
print("time to KM:",time.time()- aa)
print(KM.shape)
Yy_t = np.dot(rowY.transpose(),rowY)
Xx_t = KM
P = cvxopt.matrix(np.multiply(Yy_t,Xx_t), tc='d')
Q = -1*cvxopt.matrix(1,(m,1), tc='d')
G = cvxopt.matrix(np.vstack((-1*np.identity(m),np.identity(m))), tc='d')
H1 = np.zeros((m,1))
H2 = np.ones((m,1))
H = cvxopt.matrix(np.vstack((H1, H2)), tc='d')
A = cvxopt.matrix(rowY, tc='d')
b = cvxopt.matrix(0.0, tc='d')
model = cvxopt.solvers.qp(P,Q,G,H,A,b)
alphai = np.ravel(model['x'])
SV = np.array([x for x,y in enumerate(alphai) if y>1e-7])
print("SV:",SV,SV.shape)
points = alphai[SV]
S1 = S1[SV]
rowY = ((rowY.transpose())[SV]).transpose()
#calculation of bias
for i in range(len(points)):
b += rowY[0,i] - np.sum(points*np.squeeze(np.asarray(rowY))*KM[SV[i],SV])
b = b/SV.shape[0]
print(b)
return b,points,S1,rowY
def fit(S1,rowY):
# time1 = time.time()
m,n = S1.shape
b=0
# print(m,n)
Yy_t = np.dot(rowY.transpose(),rowY)
Xx_t = np.dot(S1,S1.transpose())
P = cvxopt.matrix(np.multiply(Yy_t,Xx_t), tc='d')
Q = -1*cvxopt.matrix(1,(m,1), tc='d')
G = cvxopt.matrix(np.vstack((-1*np.identity(m),np.identity(m))), tc='d')
H1 = np.zeros((m,1))
H2 = np.ones((m,1))
H = cvxopt.matrix(np.vstack((H1, H2)), tc='d')
A = cvxopt.matrix(rowY, tc='d')
b = cvxopt.matrix(0.0, tc='d')
model = cvxopt.solvers.qp(P,Q,G,H,A,b)
alphai = np.ravel(model['x'])
SV = np.array([x for x,y in enumerate(alphai) if y>1e-7])
print(SV)
points = alphai[SV]
S1 = S1[SV]
rowY = ((rowY.transpose())[SV]).transpose()
#calculation of weights
weight = np.zeros(n)
for n in range(len(points)):
weight += (points[n]*np.squeeze(np.asarray(S1[n]))*rowY[0,n])
#calculation of bias
for n in range(len(points)):
b -= np.dot(weight,np.squeeze(np.asarray(S1[n]))) - rowY[0,n]
b = b/SV.shape[0]
return weight,b
def predict(S2, weight,b):
a = np.zeros(S2.shape[0])
a = np.sign(np.dot(S2,weight)+b)
print(a.shape)
return a
def predictgauss(S2,b,points,S1,rowY):
a = np.zeros(S2.shape[0])
S2 = np.asarray(S2)
for j in range(S2.shape[0]):
v =0.0
for point,S,sv in zip(points,np.squeeze(np.asarray(rowY)),S1):
v += point*S*GaussianKernel(S2[j],sv)
a[j] = v+b
return np.sign(a)
def predictgaussMulti(S2,b,points,SV_S1,SV_rowY):
a = 0.0
v =0.0
for point,S,sv in zip(points,np.squeeze(np.asarray(SV_rowY)),SV_S1):
v += point*S*GaussianKernel(S2,sv)
a = v+b
return np.sign(a)
def doq(n1,n2,rowX,rowY):
S1 = np.asmatrix(rowX)/255
rowY = np.asmatrix(rowY)
b,points,SV_S1,SV_rowY = fitgaussian(S1,rowY)
return (b,points,SV_S1,SV_rowY)
def doq1(n1,n2,rowX,rowY,rowXT,rowYT,partnum,BorM,st):
###########USING CVXOPT#########################################################################################
if(partnum =="a"):
S1 = np.asmatrix(rowX)/255
S2 = np.asarray(rowXT)/255
rowY = np.asmatrix(rowY)
rowYT = np.asarray(rowYT)
#########################Linear kernel################################
weight , b = fit(S1,rowY)
rowP = predict(S2,weight,b)
print ("Accuracy = ", float(np.sum(rowP[0]==rowYT))*100/rowP[0].shape[0])
# #########################Gaussian kernel##############################
elif(partnum =="b"):
S1 = np.asmatrix(rowX)/255
S2 = np.asarray(rowXT)/255
rowY = np.asmatrix(rowY)
rowYT = np.asarray(rowYT)
print("Training...", time.time()-st)
b,points,SV_S1,SV_rowY = fitgaussian(S1,rowY)
if(BorM==1):
return (S2,b,points,SV_S1,SV_rowY,rowYT)
print("Predicting...", time.time()-st)
rowPG = predictgauss(S2,b,points,SV_S1,SV_rowY)
print ("Accuracy = ", float(np.sum(rowPG==rowYT))*100/rowPG.shape[0])
##############USING LIBSVM#################################################################################
#############Linear#########################
elif(partnum=="c"):
classes = np.asarray(rowY)
data = np.asarray(rowX)/255
classestest = np.asarray(rowYT)
datatest = np.asarray(rowXT)/255
problem = svm_problem(classes,data)
param = svm_parameter("-s 0 -t 0 -c 1.0")
model = svm_train(problem,param)
p_acc= svm_predict(classestest,datatest, model)[1]
print("Linear kernel: ",p_acc[0])
param = svm_parameter("-s 0 -t 2 -c 1.0")
model = svm_train(problem,param)
p_acc= svm_predict(classestest,datatest, model)[1]
print("Gaussian kernel: ",p_acc[0])
# print(accuracy)
# def readingT():
def reading(rowXfit,rowY,n1,n2):
rowXM = []
rowYM = []
rowXMT = []
rowYMT = []
for i in range(len(rowX)):
if(rowY[i]==n1 or rowY[i]==n2):
rowXM.append(rowX[i])
if rowY[i]==n1:
rowYM.append(1.0)
else:
rowYM.append(-1.0)
return rowXM,rowYM
def mains(st):
f0 = sys.argv[1]
f1 = sys.argv[2]
BorM = sys.argv[3]
partnum = sys.argv[4]
if(BorM =="0"):
rowX = []
rowY = []
rowXT = []
rowYT = []
i=0
with open(f0,'r') as a:
read = csv.reader(a)
for row in read:
if int(float(row[784])) == 4 or int(float(row[784])) == 5:
rowX.append(list(map(float,row[:-1])))
if int(float(row[784])) == 4 :
rowY.append(1.0)
else:
rowY.append(-1.0)
with open(f1,'r') as a:
read = csv.reader(a)
for row in read:
if int(float(row[784])) == 4 or int(float(row[784])) == 5:
rowXT.append(list(map(int,row[:-1])))
if int(float(row[784])) == 4 :
rowYT.append(1.0)
else:
rowYT.append(-1.0)
doq1(4,5,rowX,rowY,rowXT,rowYT,partnum,0,st)
elif(BorM=="1"):
if(partnum =="a"):
starttrain = time.time()
rowX = []
rowY = []
rowXT = []
rowYT = []
#
i =0
j =0
#
with open(f0,'r') as a:
read = csv.reader(a)
for row in read:
i+=1
rowX.append(list(map(float,row[:-1])))
rowY.append(int(float(row[784])))
if(i>4000):
break
print("reading done")
d ={}
for i in range(1,10):
for j in range(i):
print("training for",i,j)
rowXM,rowYM = reading(rowX,rowY,i,j)
d[str(i)+str(j)] = doq(i,j,rowXM,rowYM)
print("trained for",i,j)
print("train_complete")
endtrain = time.time()
print(endtrain - starttrain)
tot =0
correct =0
arrtrue= []
arrpredict =[]
with open(f1,'r') as a:
read = csv.reader(a)
for row in read:
a =[0]*10
rowXT = (list(map(float,row[:-1])))
S2 = np.asarray(rowXT)/255
for key in d:
(b,points,SV_S1,SV_rowY) = d[key]
rowPG = predictgaussMulti(S2,b,points,SV_S1,SV_rowY)
if (rowPG[0] == 1.0):
a[int(key[0])] +=1
else:
a[int(key[-1])] +=1
tot+=1
trueval =int(float(row[784]))
preval = a.index(max(a))
arrtrue.append(trueval)
arrpredict.append(preval)
if trueval == preval:
correct +=1
if tot%100==0:
print("partACC :",correct/tot,"for",tot)
if tot >1000:
break
print("ACC :",correct/tot)
confusion_matrix_draw(arrtrue,arrpredict)
elif(partnum =="b"):
rowX = []
rowY = []
rowXT = []
rowYT = []
with open(f0,'r') as a:
read = csv.reader(a)
for row in read:
rowX.append(list(map(float,row[:-1])))
rowY.append(int(float(row[784])))
with open(f1,'r') as a:
read = csv.reader(a)
for row in read:
rowXT.append(list(map(float,row[:-1])))
rowYT.append(int(float(row[784])))
classes = np.asarray(rowY)
data = np.asarray(rowX)/255
classestest = np.asarray(rowYT)
datatest = np.asarray(rowXT)/255
problem = svm_problem(classes,data)
param = svm_parameter("-s 0 -t 2 -c 1.0")
model = svm_train(problem,param)
p_acc= svm_predict(classestest,datatest, model)[1]
print("Gaussian kernel: ",p_acc[0])
elif(partnum == "d"):
rowX = []
rowY = []
rowXT = []
rowYT = []
with open(f0,'r') as a:
read = csv.reader(a)
for row in read:
rowX.append(list(map(float,row[:-1])))
rowY.append(int(float(row[784])))
with open(f1,'r') as a:
read = csv.reader(a)
for row in read:
rowXT.append(list(map(float,row[:-1])))
rowYT.append(int(float(row[784])))
classes = np.asarray(rowY)
data = np.asarray(rowX)/255
classesONtest = np.asarray(rowYT)
dataONtest = np.asarray(rowXT)/255
size = int(len(rowX)/100)
classestest = np.asarray(classes[-size:])
datatest = np.asarray(data[-size:])
problem = svm_problem(classes,data)
print("##################################################################")
param = svm_parameter("-s 0 -t 2 -h 0 -c 0.00001")
model = svm_train(problem,param)
p_acc= svm_predict(classestest,datatest, model)[1]
print("Gaussian kernel for C = : ",p_acc[0])
p_acc= svm_predict(classesONtest,dataONtest, model)[1]
print("Gaussian kernel for C on test dataset = : ",p_acc[0])
print("##################################################################")
param = svm_parameter("-s 0 -t 2 -h 0 -c 0.001")
model = svm_train(problem,param)
p_acc= svm_predict(classestest,datatest, model)[1]
print("Gaussian kernel for C = : ",p_acc[0])
p_acc= svm_predict(classesONtest,dataONtest, model)[1]
print("Gaussian kernel for C on test dataset = : ",p_acc[0])
print("##################################################################")
param = svm_parameter("-s 0 -t 2 -c 1.0")
model = svm_train(problem,param)
p_acc= svm_predict(classestest,datatest, model)[1]
print("Gaussian kernel for C = : ",p_acc[0])
p_acc= svm_predict(classesONtest,dataONtest, model)[1]
print("Gaussian kernel for C on test dataset = : ",p_acc[0])
print("##################################################################")
param = svm_parameter("-s 0 -t 2 -h 0 -c 5.0")
model = svm_train(problem,param)
p_acc= svm_predict(classestest,datatest, model)[1]
print("Gaussian kernel for C = : ",p_acc[0])
p_acc= svm_predict(classesONtest,dataONtest, model)[1]
print("Gaussian kernel for C on test dataset = : ",p_acc[0])
# print("##################################################################")
param = svm_parameter("-s 0 -t 2 -h 0 -c 10.0")
model = svm_train(problem,param)
p_acc= svm_predict(classestest,datatest, model)[1]
print("Gaussian kernel for C = : ",p_acc[0])
p_acc= svm_predict(classesONtest,dataONtest, model)[1]
print("Gaussian kernel for C on test dataset = : ",p_acc[0])
if __name__ == '__main__':
st = time.time()
mains(st)
print("Time Taken: ",time.time()-st)