-
Notifications
You must be signed in to change notification settings - Fork 0
/
A2Q2Validation.py
72 lines (51 loc) · 1.53 KB
/
A2Q2Validation.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
import numpy as np
import math
from svmutil import *
import time
datax = np.genfromtxt('train.csv', delimiter=',')
Y=datax[:,784]
m=Y.shape[0]
print(Y.shape)
vs=math.floor(m/10)
print(vs)
datax=np.delete(datax,784,1)
datax=datax/255.0
X=datax
idx=np.random.choice(m, m-vs, replace=False)
trainY=Y[idx]
trainX=X[idx, :]
vY = Y[[i for i in range(m) if i not in idx]]
vX = X[[i for i in range(m) if i not in idx],:]
prob=svm_problem(trainY,trainX)
m1=svm_train(prob,'-g 0.05 -t 2 -c 0.00001 -b 1')
m2=svm_train(prob,'-g 0.05 -t 2 -c 0.001 -b 1')
m3=svm_train(prob,'-g 0.05 -t 2 -c 1 -b 1')
m4=svm_train(prob,'-g 0.05 -t 2 -c 5 -b 1')
m5=svm_train(prob,'-g 0.05 -t 2 -c 10 -b 1')
print("done")
tdata = np.genfromtxt('test.csv', delimiter=',')
tY=tdata[:,784]
tdata=np.delete(tdata,784,1)
tdata=tdata/255.0
print("test!!!!")
p_labelt1, p_acct1, p_valt1 = svm_predict(tY, tdata, m1)
print(p_acct1)
p_labelt2, p_acct2, p_valt2 = svm_predict(tY, tdata, m2)
print(p_acct2)
p_labelt3, p_acct3, p_valt3 = svm_predict(tY, tdata, m3)
print(p_acct3)
p_labelt4, p_acct4, p_valt4 = svm_predict(tY, tdata, m4)
print(p_acct4)
p_labelt5, p_acct5, p_valt5 = svm_predict(tY, tdata, m5)
print(p_acct5)
print("validation!!!!")
p_labelt1, p_acct1, p_valt1 = svm_predict(vY, vX, m1)
print(p_acct1)
p_labelt1, p_acct1, p_valt1 = svm_predict(vY, vX, m2)
print(p_acct1)
p_labelt1, p_acct1, p_valt1 = svm_predict(vY, vX, m3)
print(p_acct1)
p_labelt1, p_acct1, p_valt1 = svm_predict(vY, vX, m4)
print(p_acct1)
p_labelt1, p_acct1, p_valt1 = svm_predict(vY, vX, m5)
print(p_acct1)