-
Notifications
You must be signed in to change notification settings - Fork 4
/
naive_bayes.py
31 lines (29 loc) · 1.24 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
import numpy as np
class NaiveBayes:
def __init__(self, dataset, smoothing_constant):
self.dataset = dataset
self.num_classes = len(self.dataset.labels_dict)
self.smoothing_constant = smoothing_constant
def get_nb_matrix(self):
weight_mat = np.zeros((self.num_classes, self.num_classes))
for count, i in enumerate(self.dataset.data):
labels = i['labels']
relations = {}
for current in labels:
for other in labels:
if other is not current:
try:
relations[(current,other)] += 1
except:
relations[(current,other)] = 1
for key in relations.keys():
i = self.dataset.labels_dict[key[0]]
j = self.dataset.labels_dict[key[1]]
weight_mat[i,j] += 1
smoothing = np.full((self.num_classes, self.num_classes),
self.smoothing_constant)
dividend = weight_mat + smoothing
class_probabilties = np.sum(weight_mat, axis=0)
divisor = class_probabilties + smoothing*len(self.dataset.labels_dict)
nb_mat = dividend / divisor
return nb_mat