-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (50 loc) · 1.37 KB
/
main.py
File metadata and controls
81 lines (50 loc) · 1.37 KB
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
import numpy, idx2numpy
from sklearn.svm import LinearSVC
y = idx2numpy.convert_from_file("train-labels-idx1-ubyte")
X = idx2numpy.convert_from_file('train-images-idx3-ubyte')
Xp = []
for x in X:
Xp.append(x.flatten())
Xp = numpy.matrix(Xp)
U,s,VT = numpy.linalg.svd(Xp,full_matrices = False)
XpTXp = VT.T @ (numpy.diag(s)) @ VT
print(XpTXp.shape)
XpTXpi = numpy.linalg.inv(XpTXp)
XpTXpiT = XpTXpi@(Xp.T)
def linear_classifier_for_digit(d,XpTXpiXT,y):
yp = []
for i in y:
if(i == d):
yp.append(1)
else:
yp.append(-1)
w = XpTXpiXT@yp
return w
# w0 = linear_classifier_for_digit(0,XpTXpiT,y)
w = []
for i in range(10):
#w.append(linear_classifier_for_digit(i,XpTXpiT,y))
pass
# ridge classifier for parameter labmda
def ridge_classifier_for_digit(d,Xp,XpTXp,y,lam):
yp = []
for i in y:
if(i == d):
yp.append(1)
else:
yp.append(-1)
XR = numpy.linalg.inv(XpTXp + lam*numpy.identity(784))
w = XR@(Xp.T)@yp
return w
#rw = ridge_classifier_for_digit(0,Xp,XpTXp,y,1)
def supportvector_classifier_for_digit(d,Xp,y):
yp = []
for i in y:
if(i == d):
yp.append(1)
else:
yp.append(-1)
clf = LinearSVC(random_state=0, tol=1e-8)
clf.fit(Xp, yp)
w_opt = clf.coef_.transpose()
return w_opt