-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn-dist.py
141 lines (112 loc) · 4.18 KB
/
knn-dist.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
#!/usr/bin/env python
# -*- coding: utf-8 -*
import numpy as np
import numpy as np
import theano.tensor as T
import theano
import time
import sys
class Timer(object):
def __init__(self):
self.startTime = time.time()
self.curTime = time.time()
def getTime(self):
self.curTime = time.time()
return "Total time: %f sec" %(self.curTime - self.startTime)
def getTimeGap(self):
t = time.time()
gap = t - self.curTime
self.curTime = t
return " %f sec" %(gap)
tokenized_doc_loc = "/media/joseph/SSD/TrainingData_docVec_jieba/"
def knn_training(file_path):
superClass = ["運動","娛樂","生活","社會","地方","要聞","兩岸","全球","產經","股市", "即時"]
subClass = ["運動","娛樂","生活","社會","地方","要聞","兩岸","國際","財經"]
with open(file_path, 'r') as dr:
doc_list = dr.read().splitlines()
doc_list = [x.split(' ') for x in doc_list]
#caculate normalized term-frequency of features for all training documents
train_vec = []
for i in range(len(doc_list)):
with open(tokenized_doc_loc + doc_list[i][0], 'r') as dr:
vec = dr.read().split(' ')
vec = [float(k) for k in vec]
train_vec.append(vec)
train_vec = np.asmatrix(train_vec, dtype = 'float32')
train_class = []
for i in range(len(doc_list)):
sup_index = superClass.index(doc_list[i][1])
if sup_index == 10:
sub_index = subClass.index(doc_list[i][2])
train_class.append(sub_index)
else:
train_class.append(sup_index)
return [train_vec, train_class]
def knn_testing(file_path, _knn_test, train_class, K_NUM):
superClass = ["運動","娛樂","生活","社會","地方","要聞","兩岸","全球","產經","股市", "即時"]
subClass = ["運動","娛樂","生活","社會","地方","要聞","兩岸","國際","財經"]
with open(file_path, 'r') as dr:
doc_list = dr.read().splitlines()
doc_list = [x.split(' ') for x in doc_list]
#produce target output (real classes for testing document)
target_out = []
for i in range(len(doc_list)):
sup_index = superClass.index(doc_list[i][1])
if sup_index == 10:
sub_index = subClass.index(doc_list[i][2])
target_out.append(sub_index)
else:
target_out.append(sup_index)
#produce predict output (predict classes for testing document)
predict_out = []
drange = int(len(doc_list) / 50)
correct_num, total_num = [0., 0.]
test_vec = []
for i in range(len(doc_list)):
with open(tokenized_doc_loc + doc_list[i][0], 'r') as dr:
test_vec = dr.read().split(' ')
test_vec = [float(k) for k in test_vec]
test_vec = np.asarray(test_vec, dtype = 'float32')
y = _knn_test(test_vec)
y_index = y.argsort()[:K_NUM]
vote_class = np.full(10, 0, 'int')
for index in y_index:
vote_class[train_class[index]] += 1
predict_class = np.argmax(vote_class)
predict_out.append(predict_class)
if predict_class == target_out[i]:
correct_num += 1
total_num += 1
if i % drange == 0:
p = int(i / drange)
sys.stdout.write('\r')
sys.stdout.write("[%-51s] %d%% accu: %f" % ('='*p+'>', 2*p, (correct_num / total_num)))
sys.stdout.flush()
correct_num, total_num = [0., 0.]
return [predict_out, target_out]
if __name__ == '__main__':
train_file = ['100t', '50t', '20t', '10t', '5t', '2t', '1t']
for tname in train_file:
K_NUM = 15
timer = Timer() ###
train_vec, train_class = knn_training('../svm/doc_result/doc_merge_vec_train_' + tname + '.txt')
print 'doc_merge_vec_train_' + tname + '.txt'
#theano function declaration
ntv = theano.shared(np.asarray(train_vec, dtype = 'float32'))
train_vec = None
x = T.vector(dtype="float32")
y = T.power(ntv- x, 2).sum(axis=1)
_knn_test = theano.function(inputs = [x], outputs = y)
predict_out, target_out = knn_testing('../svm/doc_result/doc_merge_vec_test_200t.txt', _knn_test, train_class, K_NUM)
print "Testing: " + timer.getTime()
#Analysis result
result = np.full(shape = (10, 10), fill_value = 0, dtype = 'int')
for t, p in zip(target_out, predict_out):
result[t, p] += 1
correct_num = 0
for i in range(10):
correct_num += result[i][i]
print "K= %d F1= %f"%(K_NUM, float(correct_num) / np.asarray(result).sum())
print "Print Contigency Table"
for i in range(len(result)):
print ' '.join([str(x) for x in result[i]])