/
Base.py
278 lines (187 loc) · 5.69 KB
/
Base.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
from numpy import *
from numberClass import *
from training import *
from testing import *
from Document import *
from emailClass import *
from Bayes import *
classes = (10)
def readTrainingLabels():
# Open The File
file = open("digitdata/traininglabels.txt" , "r")
# Count each digit in training set
list = [0,0,0,0,0,0,0,0,0,0]
labels = []
# Total num of digits
counter = 0
for line in file:
list[int(line.strip())]+=1
counter+=1
labels.append(int(line.strip()))
file.close()
return [float(x)/counter for x in list],labels
def readTrainingImages():
# Open the Training Images
file = open("digitdata/trainingimages.txt", "r")
# The List to store the images
list = []
curr_image = zeros((28,28))
i = 0
for line in file:
# Remove the \n
line = line.rstrip()
j = 0
for character in line:
if line[j] != ' ':
curr_image[(i)%28][j] = 1
j+=1
i+=1
if (i%28) == 0:
list.append(curr_image)
curr_image = zeros((28,28))
return list
def readTestingLabels():
# Open The File
file = open("digitdata/testlabels.txt" , "r")
# Count each digit in training set
list = [0,0,0,0,0,0,0,0,0,0]
labels = []
# Total num of digits
counter = 0
for line in file:
list[int(line.strip())]+=1
counter+=1
labels.append(int(line.strip()))
file.close()
return list,labels
def readTestingImages():
# Open the Training Images
file = open("digitdata/testimages.txt", "r")
# The List to store the images
list = []
curr_image = zeros((28,28))
i = 0
for line in file:
# Remove the \n
line = line.rstrip()
j = 0
for character in line:
if line[j] != ' ':
curr_image[(i)%28][j] = 1
j+=1
i+=1
if (i%28) == 0:
list.append(curr_image)
curr_image = zeros((28,28))
return list
def part1():
#priorList = readTrainingLabels()
imagesList = readTrainingImages()
labelsList, labels = readTrainingLabels()
classesList = []
for x in xrange(0,10):
classesList.append(numberClass(x))
classesList[x].setPrior(labelsList[x])
for x in xrange(0,len(imagesList)):
classesList[labels[x]].addTrainingData(imagesList[x])
for i in xrange(1,2):
for x in xrange(0,10):
classesList[x].empirical_likelihood = smoothed_likelihood(classesList[x].training_data,i)
testingImagesList = readTestingImages()
hypotheticalLabels = []
confusionMatrix = zeros((10,10))
for x in xrange(0, len(testingImagesList)):
hypotheticalLabels.append(numClassifier(classesList,testingImagesList[x]))
hypotheticalClasses = [0,0,0,0,0,0,0,0,0,0]
for element in hypotheticalLabels:
hypotheticalClasses[element]+=1
#print hypotheticalClasses
testClasses, testLabels = readTestingLabels()
error = list(array(hypotheticalLabels) - array(testLabels))
error_by_class = []
for x in xrange(0,10):
error_by_class.append(100 - abs(float(hypotheticalClasses[x]-testClasses[x])*100/testClasses[x]))
# Find the confusion matrix
for x in xrange(0,len(testLabels)):
confusionMatrix[testLabels[x]][hypotheticalLabels[x]] += 1
for x in xrange(0,10):
for y in xrange(0,10):
confusionMatrix[x][y] = confusionMatrix[x][y] * 100 / testClasses[x]
error_value = float(count_nonzero(error))/10
file = open("sample.txt" , "w")
for i in xrange(0,10):
print "Digit Class: ", i
print "Highest Posterior", classesList[i].highestPosterior
print classesList[i].highPostImage
print "Lowest Posterior", classesList[i].lowestPosterior
print classesList[i].lowPostImage
#print "The error is ", error_value
#print "Success Rate: ", int(100-error_value), " for a value of k: ", i
#print "Classification Rate: ", error_by_class
#print confusionMatrix
#print "This is the priors: ",labelsList, " for a smoothing of: ", i
#print "This is the actual stats: ",testClasses, " for a smoothing of: ", i
#print "This is the hypothetical stats: ",hypotheticalClasses, " for a smoothing of: ", i
#print "Error By Digit: ", error_by_class
#print "This is the likelihood: ", classesList[x].empirical_likelihood
def readTrainingEmails():
#Open the training emails
file = open("spamdata/train_email.txt", "r")
emails = []
for line in file:
linelist = line.split()
dictemails = {}
for x in xrange(1,len(linelist)):
a,b = linelist[x].split(":")
dictemails[a] = int(b)
emails.append(Document(dictemails,labelvalue = int(linelist[0])))
file.close()
return emails
def readTestingEmails():
#Open the training emails
file = open("spamdata/test_email.txt", "r")
actuallabels = []
testingemails= []
for line in file:
linelist = line.split()
dictemails = {}
for x in xrange(1,len(linelist)):
a,b = linelist[x].split(":")
dictemails[a] = int(b)
actuallabels.append(int(linelist[0]))
testingemails.append(Document(dictemails))
file.close()
return actuallabels,testingemails
def part2():
training_emails = readTrainingEmails()
spam_emails = []
reg_emails = []
for email in training_emails:
if(email.label == 0):
reg_emails.append(email)
else:
spam_emails.append(email)
emails_classes = []
emails_classes.append(emailClass(reg_emails))
emails_classes.append(emailClass(spam_emails))
multinomial(emails_classes[0])
multinomial(emails_classes[1])
bernouilli(emails_classes[0])
bernouilli(emails_classes[1])
file = open("multinomial_regular.txt", "w")
print>>file, emails_classes[0].m_likelihood
file.close()
file = open("multinomial_spam.txt", "w")
print>>file, emails_classes[1].m_likelihood
file.close()
file = open("bernouilli_regular.txt", "w")
print>>file, emails_classes[0].b_likelihood
file.close()
file = open("bernouilli_spam.txt", "w")
print>>file, emails_classes[1].b_likelihood
file.close()
actual_labels, testing_emails = readTestingEmails()
def main():
part2()
if __name__ == '__main__':
main()