-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathNaiveBayes.py
37 lines (32 loc) · 954 Bytes
/
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
from numpy import *
dataSet = [[0, 0], [0, 0], [0, 0], [0, 0], [1, 0], [0, 0], [1, 0], [0, 0], [1, 0], [1, 0], [1, 1], [1, 0], [1, 1],
[1, 1], [1, 1], [1, 1], [1, 1], [1, 1]]
ySet = [0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
def train():
dataNum = len(dataSet)
featureNum = len(dataSet[0])
p0Num = ones(featureNum)
p1Num = ones(featureNum)
p0Denom = 2.0
p1Denom = 2.0
p0 = 0
for i in range(dataNum):
if ySet[i] == 1:
p1Num += dataSet[i]
p1Denom += sum(dataSet[i])
else:
p0 += 1
p0Num += dataSet[i]
p0Denom += sum(dataSet[i])
p0Rate = p0 / dataNum
p0Vec = log(p0Num / p0Denom)
p1Vec = log(p1Num / p1Denom)
return p0Rate, p0Vec, p1Vec
p0Rate, p0Vec, p1Vec = train()
test = [1, 0]
p1 = sum(test * p1Vec) + log(1.0 - p0Rate)
p0 = sum(test * p0Vec) + log(p0Rate)
if p1 > p0:
print(1)
else:
print(0)