-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_bayes.py
52 lines (47 loc) · 1.83 KB
/
naive_bayes.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
import control as co
from frequency_table_create import frequency_table_class as ft
class naive_bayes(ft):
# storing probability of each group of score stored in list
probability_of_outputs = list(0 for i in range(co.score_group_num))
dataset_length = 0
def init(self):
for i in range(co.score_group_num):
self.dataset_length += self.fre_table['popularity'][i][i]
# calculate each item's independent probability
for i in range(co.score_group_num):
self.probability_of_outputs[i] = float(self.fre_table['popularity'][i][i] / self.dataset_length)
# print(self.probability_of_outputs)
def get_result(self, input):
self.init()
result = list(1 for _ in range(co.score_group_num))
for i in range(co.score_group_num):
for k in co.feature_keys:
if k == 'popularity' or k not in input.keys():
continue
x = self.fre_table[k][input[k]][i]
# if somethings value is zero we can increment all related value
# and it will not affect the result
if x == 0:
x = 1
y = self.fre_table['popularity'][i][i]
try:
result[i] *= x / y
except ZeroDivisionError:
continue
result[i] *= self.probability_of_outputs[i]
sum = 0
for i in result:
sum += i
for i in range(co.score_group_num):
try:
result[i] = result[i] / sum
except ZeroDivisionError:
continue
sum = 0
ans, mx = -1, -1
for i in range(co.score_group_num):
sum += result[i]
if mx < result[i]:
mx = result[i]
ans = i
return ans, mx