-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberClass.py
51 lines (44 loc) · 1.77 KB
/
NumberClass.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
from math import *
import numpy as np
from operator import itemgetter, attrgetter
class NumberClass:
def __init__(self):
#Turn into dictionary of the form (word category, list of words within that category
self.Feature = []
self.likelihoods1 = []
self.likelihoods0 = []
self.count = 0
self.MAP = 0
for i in range(784):
self.Feature.append(0)
self.likelihoods1.append(0)
self.likelihoods0.append(0)
def addToFeature(self,adder, positiony, positionx):
self.Feature[positionx+positiony*28] += adder
def addToCount(self, adder):
self.count += adder
def getFeature (self, positiony, positionx):
FeatureValue = self.Feature[positionx+28*positiony]
return FeatureValue
def getCount(self):
countValue = self.count
return countValue
def getMAP(self, number):
print "Counts", self.count
self.MAP += np.log(float(self.count)/number)
for row in range(28):
for column in range(28):
value = float(self.Feature[column+28*row])/self.count
self.MAP += np.log(value)
self.likelihoods1[row*28+column] = np.log(value)
self.likelihoods0[row*28+column] = np.log10(1-value)
return self.MAP
def getTestScore(self, featureVector):
score = np.log(float(self.count)/5000)
for row in range(28):
for column in range(28):
if featureVector[row*28+column] != 0:
score += self.likelihoods1[row*28+column]
else:
score += self.likelihoods0[row+28*column]
return score