-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaiveBayes.py
64 lines (54 loc) · 2.29 KB
/
naiveBayes.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
dictionary = ["free", "try", "offer", "trial", "credit", "single", "clearance", "earnings", "cheap", "dollars", "debt", "traffic"]
spam = [
[0,1,0,1,0,1,0,1,0,1,0,1],
[1,1,1,1,1,0,0,0,0,1,0,0],
[1,0,0,0,0,1,0,0,0,0,0,0],
[1,0,0,0,0,1,0,0,0,0,0,0],
[1,0,0,0,0,1,0,0,0,0,0,0],
[1,0,0,0,0,1,0,0,0,0,0,0]
]
nonspam = [
[0,1,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,1,0,0],
[0,0,0,0,1,0,0,0,0,1,1,0]
]
def spamClassifier(plusSample, negativeSample, dictionaryLength, testExample):
noPlusExamples = len(plusSample)
noNegativeExamples = len(negativeSample)
plusProbabilityDistribution = laplaceSmooth(getProbabilityDistribution(plusSample), noPlusExamples)
negativeProbabilityDistribution = laplaceSmooth(getProbabilityDistribution(negativeSample), noNegativeExamples)
probabilityPositiveExample = float(noPlusExamples) / (noPlusExamples + noNegativeExamples)
probabilityDataGivenPlusLabel = getProbabilityDataGivenLabel(testExample, plusProbabilityDistribution)
numerator = probabilityDataGivenPlusLabel * probabilityPositiveExample
probabilityDataGivenNegativeLabel = getProbabilityDataGivenLabel(testExample, negativeProbabilityDistribution)
denominator = numerator + (probabilityDataGivenNegativeLabel * (1-probabilityPositiveExample))
return numerator / denominator
def getProbabilityDistribution(sample):
noTrainingExamples = len(sample)
noFeatures = len(sample[0])
probabilityDistribution = [0] * noFeatures
for i in range(noTrainingExamples):
for j in range(noFeatures):
probabilityDistribution[j] += float(sample[i][j]) / noTrainingExamples
return probabilityDistribution
def getProbabilityDataGivenLabel(trainingExample, probabilityDistribution):
p = 1
for i in range(len(trainingExample)):
if trainingExample[i] == 0:
p = p * (1-probabilityDistribution[i])
else:
p = p * probabilityDistribution[i]
return p
def laplaceSmooth(probabilityDistribution, sampleSize):
for i in range(len(probabilityDistribution)):
p = probabilityDistribution[i]
p = ((p * sampleSize)+1)/(sampleSize + 2)
probabilityDistribution[i] = p
return probabilityDistribution
for i in range(len(spam)):
print spamClassifier(spam, nonspam, 12, spam[i])
for i in range(len(nonspam)):
print spamClassifier(spam, nonspam, 12, nonspam[i])