-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpredict.py
More file actions
49 lines (44 loc) · 1012 Bytes
/
predict.py
File metadata and controls
49 lines (44 loc) · 1012 Bytes
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
import os
import sys
# An example in that book, the training set and parameters' sizes are fixed
training_set = []
w = []
b = 0
lens = 0
n = 0
def cal(item):
global w, b, lens
res = 0
for i in range(lens):
res += item[i] * w[i]
res += b
return res
if __name__=="__main__":
if len(sys.argv) != 4:
print "Usage: python predict.py testFile modelFile outFile"
exit(0)
testFile = file(sys.argv[1])
modelFile= file(sys.argv[2])
outFile = file(sys.argv[3], 'w')
line1 = modelFile.readline().strip()
line2 = modelFile.readline().strip()
line3 = modelFile.readline().strip()
line4 = modelFile.readline().strip()
chunk = line1.strip().split(' ')
for key in chunk:
w.append(float(key))
b = float(line2)
lens = int(line3)
n = float(line4)
for line in testFile:
chunk = line.strip().split(' ')
tmp = []
for i in range(1, lens+1):
tmp.append(int(chunk[i]))
re = cal(tmp)
if re > 0:
outFile.write('+1\n')
else:
outFile.write('-1\n')
outFile.close()
testFile.close()