-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw5.py
120 lines (94 loc) · 2.87 KB
/
hw5.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
import sys
import random
def gradient_descent(data, trainLabels, rows, cols):
w = []
for i in range(0, cols):
w.append(0.02 * random.random() - 0.01)
emp_risk = 0
diff = 1
eta=0.001
while (diff > 0.001):
prev=emp_risk
dellf = [0] * cols
# compute gradient
for i in range(0, rows, 1):
if (trainLabels.get(i) != None):
a = trainLabels[i] * dot(w, data[i])
for j in range(0, cols):
if a<1:
dellf[j] += -(trainLabels[i] * data[i][j])
else:
dellf[j] += 0
# adaptive hinge
eta_list = [1, .1, .01, .001, .0001, .00001, .000001, .0000001, .00000001, .000000001, .0000000001, .00000000001]
bestobj = 1000000000000
for k in range(0, len(eta_list), 1):
eta = eta_list[k]
# update w
for j in range(0, cols, 1):
w[j] -= eta * dellf[j]
# calculate error
emp_risk = 0
for i in range(0, rows,1):
if (trainLabels.get(i) != None):
emp_risk += max(0, 1 - (trainLabels.get(i)) * dot(w, data[i]))
obj = emp_risk
if obj < bestobj:
bestobj = obj
best_eta = eta
# update w
for j in range(0, cols, 1):
w[j] += eta * dellf[j]
if best_eta != None:
eta = best_eta
# update w
for j in range(0, cols, 1):
w[j] -= eta * dellf[j]
emp_risk = 0
for i in range(0, rows):
if (trainLabels.get(i) != None):
emp_risk += max(0, 1 - (trainLabels.get(i)) * dot(w, data[i]))
diff = abs(prev - emp_risk)
return w
def dot(a, b):
list1 = []
for x, y in zip(a, b):
mul = x * y
list1.append(mul)
return sum(list1)
if __name__ == '__main__':
datafile = sys.argv[1]
f = open(datafile)
data = []
i = 0
l = f.readline()
while (l != ''):
a = l.split()
l2 = []
for j in range(0, len(a), 1):
l2.append(float(a[j]))
data.append(l2)
l = f.readline()
labelfile = sys.argv[2]
f = open(labelfile)
trainLabels = {}
l = f.readline()
while (l != ''):
a = l.split()
trainLabels[int(a[1])] = int(a[0])
l = f.readline()
for i in trainLabels:
if trainLabels[i] == 0:
trainLabels[i] = -1
for i in range(0, len(data)):
data[i].append(1)
rows = len(data)
cols = len(data[0])
w = gradient_descent(data, trainLabels, rows, cols)
for i in range(0, rows):
if (trainLabels.get(i) == None):
dp = dot(w, data[i])
if (dp > 0):
print('1', i)
else:
print('0', i)