-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.py
288 lines (241 loc) · 8.52 KB
/
part1.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import numpy as np
from copy import copy, deepcopy
import time
from collections import defaultdict as setdefault
# import matplotlib.pyplot as plt
def main():
#=======initialize================
start_time = time.time()
traininglabels = []
testlabels = []
trainingprior = np.zeros((10))
likelihood = []
testresult = []
NavieDic = {}
#========Reading==================
with open('traininglabels') as f:
content = f.readlines()
traininglabels = [int(x.strip('\n')) for x in content]
with open('testlabels') as f:
content = f.readlines()
testlabels = [int(x.strip('\n')) for x in content]
trainingset = {} # P(piror) = trainingprior[i] # of class[i] = trainingset
traininglength = len(traininglabels)
for i in range(10):
count = traininglabels.count(i)
trainingset[i] = count
trainingprior[i] = count/traininglength
likelihood.append([])
with open('trainingimages') as f:
content = f.readlines()
Building(content, traininglabels, NavieDic)
end = time.time()
print("Training Time: ", end - start_time)
#===========Testing================================================
with open('testimages') as f:
content = f.readlines()
# poss_laplace = [0.1, 0.5, 1, 3, 5]
poss_laplace = [0.5]
for i in range(len(poss_laplace)):
pos_low = np.zeros(10)
pos_high = np.zeros(10)
start_time = time.time()
pos_high, pos_low = NavieClassify(content, NavieDic, testresult, trainingset, trainingprior, \
poss_laplace[i], pos_low, pos_high, testlabels)
end = time.time()
highlow(content, pos_high, pos_low)
correct_predicted = len([i for i, j in zip(testlabels, testresult) if i == j])
Accuracy_rate = (correct_predicted/len(testlabels))*100
print("Total Accuracy (Laplace = ", poss_laplace[i], "): ", Accuracy_rate, "%")
report_digit_accuracy(testresult, testlabels, NavieDic, trainingset)
print("Testing Time: ", end - start_time)
testresult = []
def highlow(content, pos_high, pos_low):
f = open('output.txt','w')
for i in range(10):
low = int(pos_low[i])
high = int(pos_high[i])
for c in range(low*28, (low+1)*28):
f.write(content[c])
for c in range(high*28, (high+1)*28):
f.write(content[c])
f.close()
def report_digit_accuracy(testresult, testlabels, NavieDic, trainingset):
digit = np.zeros((10))
correct_digit = np.zeros((10))
confusion = np.zeros((11, 11))
confusion[0, 1:] = np.arange(10)
confusion[1:, 0] = np.arange(10)
for i in range(len(testresult)):
digit[testlabels[i]] += 1
if testresult[i] == testlabels[i]:
correct_digit[testlabels[i]] += 1
confusion[testlabels[i]+1][testresult[i]+1] += 1
print("Classification Rate for Digits 0 ~ 9:")
for i in range(10):
print(i, ": ", "{0:.2f}".format(100*correct_digit[i]/digit[i]), "%", end = " ")
if i == 3 or i == 6:
print()
print()
confusion_mtx(testresult, testlabels, digit, confusion, NavieDic, trainingset)
def confusion_mtx(testresult, testlabels, digit, confusion, NavieDic, trainingset):
for i in range(1, 11):
confusion[i, 1:] /= digit[i-1]
print()
np.set_printoptions(precision=2)
print(confusion)
print()
confusion = confusion[1:, 1:]
ratiolist = []
for i in range(20):
row = np.argmax(np.max(confusion, axis=1))
col = np.argmax(np.max(confusion, axis=0))
ratiolist.extend([(row, col)])
confusion[row, col] = 0
ratiolist = ratiolist[10:14]
print("With Laplace = 0.5, Top 4 Pairs with Highest Odd_Ratio:")
print(ratiolist[0:4])
# ratio(NavieDic, ratiolist, trainingset)
def ratio(NavieDic, ratiolist, trainingset):
for tup in ratiolist:
print(tup)
oddratiomtx = np.zeros((28, 28))
mat1 = np.zeros((28,28))
mat2 = np.zeros((28,28))
num1 = tup[0]
num2 = tup[1]
for i in range(28):
for j in range(28):
loc = (i, j)
num1count = (NavieDic[loc]['+'][num1] + NavieDic[loc]['#'][num1]+1)/trainingset[num1]
num2count = (NavieDic[loc]['+'][num2] + NavieDic[loc]['#'][num2]+1)/trainingset[num2]
probability = num1count/num2count
oddratiomtx[i, j] = probability
mat1[i,j] = num1count
mat2[i,j] = num2count
heatmap(mat1, likelihood=True)
heatmap(mat2, likelihood=True)
heatmap(oddratiomtx)
def heatmap(matrix, likelihood=False):
if likelihood:
partitions = [.25, .5, .75]
else:
nmatrix = sorted(matrix.flatten())
part_val = 28*28/5
partitions = [nmatrix[int(part_val)], nmatrix[int(part_val*2)]
, nmatrix[int(part_val*3)]]
# import IPython
# IPython.embed()
# exit()
for row in matrix:
for val in row:
if val < partitions[0]:
print('@', end='')
elif val > partitions[0] and val < partitions[1]:
print('+', end='')
elif val > partitions[1] and val < partitions[2]:
print('-', end='')
else:
print('.', end='')
print()
print()
#=============================Binary Features=====================================
def Building(content, traininglabels, NavieDic):
for i in range(int(len(content)/28)):
cur_label = traininglabels[i]
for row in range(i*28, (i+1)*28):
for col in range(28):
loc = (row%28, col) # location of that grid
cur_char = content[row][col] # character in that grid
if loc not in NavieDic: # not in the dictionary yet
NavieDic.setdefault(loc, {})
NavieDic[loc][' '] = np.zeros((10))
NavieDic[loc]['+'] = np.zeros((10))
NavieDic[loc]['#'] = np.zeros((10))
NavieDic[loc][cur_char][cur_label] += 1
else:
NavieDic[loc][cur_char][cur_label] += 1
# P(piror) = trainingprior[i]
# Number of class[i] = trainingset[i]
def NavieClassify(content, NavieDic, testresult, trainingset, trainingprior, laplace_const, pos_low, pos_high, testlabels):
pos_low_value = np.zeros(10)
pos_low_value[:] = np.inf
pos_high_value = np.zeros(10)
pos_high_value[:] = -np.inf
for c in range(int(len(content)/28)):
actual_label = testlabels[c]
posteriori = [np.log(trainingprior[i]) for i in range(10)]
for row in range(c*28, (c+1)*28):
for col in range(28):
loc = (row%28, col) # location of that grid
cur_char = content[row][col] # character in that grid
classlist = NavieDic[loc][cur_char]
for i in range(10):
total_num = trainingset[i]
lkhood = classlist[i]
if lkhood == 0:
lkhood = laplace_const
total_num += laplace_const * 2
posteriori[i] += np.log(lkhood/total_num)
max_value = max(posteriori)
max_label = posteriori.index(max(posteriori))
if max_label == actual_label:
if(pos_low_value[max_label] > max_value):
pos_low_value[max_label] = max_value
pos_low[max_label] = c
if(pos_high_value[max_label] < max_value):
pos_high_value[max_label] = max_value
pos_high[max_label] = c
testresult.extend([max_label])
return pos_high, pos_low
#=============================Ternary Features=====================================
# def Building(content, traininglabels, NavieDic):
# for i in range(int(len(content)/28)):
# cur_label = traininglabels[i]
# for row in range(i*28, (i+1)*28):
# for col in range(28):
# loc = (row%28, col) # location of that grid
# cur_char = content[row][col] # character in that grid
# if cur_char == '+':
# cur_char = '#'
# if loc not in NavieDic: # not in the dictionary yet
# NavieDic.setdefault(loc, {})
# NavieDic[loc][' '] = np.zeros((10))
# NavieDic[loc]['#'] = np.zeros((10))
# NavieDic[loc][cur_char][cur_label] += 1
# else:
# NavieDic[loc][cur_char][cur_label] += 1
# # P(piror) = trainingprior[i]
# # Number of class[i] = trainingset[i]
# def NavieClassify(content, NavieDic, testresult, trainingset, trainingprior, laplace_const, pos_low, pos_high):
# pos_low_value = np.zeros(10)
# pos_low_value[:] = np.inf
# pos_high_value = np.zeros(10)
# pos_high_value[:] = -np.inf
# for c in range(int(len(content)/28)):
# posteriori = [np.log(trainingprior[i]) for i in range(10)]
# for row in range(c*28, (c+1)*28):
# for col in range(28):
# loc = (row%28, col) # location of that grid
# cur_char = content[row][col] # character in that grid
# if cur_char == '+':
# cur_char = '#'
# classlist = NavieDic[loc][cur_char]
# for i in range(10):
# total_num = trainingset[i]
# lkhood = classlist[i]
# if lkhood == 0:
# lkhood = laplace_const
# total_num += laplace_const * 2
# posteriori[i] += np.log(lkhood/total_num)
# max_value = max(posteriori)
# max_label = posteriori.index(max(posteriori))
# if(pos_low_value[max_label] > max_value):
# pos_low_value[max_label] = max_value
# pos_low[max_label] = c
# if(pos_high_value[max_label] < max_value):
# pos_high_value[max_label] = max_value
# pos_high[max_label] = c
# testresult.extend([max_label])
# return pos_high, pos_low
main()