-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnaive_bayes.py
151 lines (114 loc) · 5.03 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import numpy as np
from ..base import BaseClassifier
class BernoulliNaiveBayes(BaseClassifier):
''' Bernouilly naive Bayes classifier
Parameters
----------
encoding : string,
Type of input data to consider: categorial (Boolean Matrix) or object (ex : list of words)
'''
def __init__(self,encoding="categorical"):
assert (encoding in ["object","categorical"]), "Encoding must either be object or categorical"
self.cond_prob = None
self.prior = None
self.features = None
self.labels = None
self.encoding = encoding
def fit(self,X,y):
'''
if encoding = object :
X : list of list of object (ex words)
y : list of corresponding labels
elif encoding = categorical :
X : numpy array of boolean
y : list of corresponding labels
'''
self.labels,Nk = np.unique(y,return_counts=True)
self.prior = Nk/len(y) # Called rho_{k} in ML2
if self.encoding == 'object' :
self.features = list(set([ xi for x in X for xi in x])) # List of unique features in X
#Computing the conditionnal probabilities
self.cond_prob = dict() # Called Pi_{ki} in ML2
i=0
for l in self.labels : # for each label
Xk = np.array(X)[np.array(y) == l] # training features corresponding to this label
self.cond_prob[l] = dict()
for f in self.features : self.cond_prob[l][f]=(1./ (Nk[i]+2)) # init features count
for x in Xk :
for f in np.unique(x) :
self.cond_prob[l][f] += (1./ (Nk[i]+2))
i+=1
elif self.encoding == 'categorical' :
self.cond_prob = np.zeros((len(self.labels),X.shape[1]))
for i in range(len(self.labels)) :
Xl = X[np.array(y) == self.labels[i]]
self.cond_prob[i] = (1+np.sum(Xl,axis=0))/(Nk[i]+2)
def predict(self,X):
if self.encoding == 'object' :
y_hat = []
for x in X :
probs = []
for i in range(len(self.labels)):
l = self.labels[i]
prob_k = np.log(self.prior[i]) # Called y_k in ML2, add the log of prior
for f in self.features :
if f in x :
prob_k += np.log(self.cond_prob[l][f])
else :
prob_k += np.log(1. - self.cond_prob[l][f])
probs.append(prob_k)
y_hat.append(self.labels[np.argmax(probs)])
elif self.encoding == 'categorical':
# see sklearn.utils.extmath.safe_sparse_dot
probs = np.log(self.prior) + X @ np.log(self.cond_prob.T)# + (1-X)@np.log(1-self.cond_prob.T)
y_hat = self.labels[np.argmax(probs,axis=1)]
return y_hat
class GaussianNaiveBayes(BaseClassifier):
''' Gaussian naive Bayes classifier
Parameters
----------
var_smoothing : float, default=0.001
Smoothing parameter (0 for no smoothing).
'''
def __init__(self,var_smoothing=1e-9):
self.prior = None
self.labels = None
self.mu = None
self.std = None
self.var_smoothing = var_smoothing
def fit(self,X,y):
self.labels,Nk = np.unique(y,return_counts=True)
self.prior = Nk/len(y)
self.mu = np.zeros((len(self.labels),X.shape[1]))
self.std = np.zeros((len(self.labels),X.shape[1]))
for i in range(len(self.labels)) :
Xl = X[np.array(y) == self.labels[i]]
self.mu[i] = np.sum(Xl,axis=0) / Nk[i]
#In some case may need to add some smoothing (see sklearn)
self.std[i] = np.std(Xl,axis=0)**2 + self.var_smoothing
def predict(self,X):
probs = np.zeros((X.shape[0],len(self.labels))) +np.log(self.prior)
for i in range(len(self.labels)):
probs[:,i] += - 0.5 *np.sum( np.log(2*np.pi*self.std[i]) + (X-self.mu[i])**2/self.std[i],axis=1)
y_hat = self.labels[np.argmax(probs,axis=1)]
return y_hat
class MultinomialNaiveBayes(BaseClassifier):
''' Multinomial naive Bayes classifier
Parameters
----------
alpha : float, default=0.001
Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
'''
def __init__(self,alpha=0.001):
self.alpha = alpha
def fit(self,X,y):
self.labels,Nk = np.unique(y,return_counts=True)
self.prior = Nk/len(y) # Called rho_{k} in ML2
self.cond_prob = np.zeros((len(self.labels),X.shape[1]))
for i in range(len(self.labels)) :
Xl = X[np.array(y) == self.labels[i]]
self.cond_prob[i] = (self.alpha+np.sum(Xl,axis=0))/(Nk[i]+X.shape[1]+1)
def predict(self,X):
probs = np.log(self.prior) + X @ np.log(self.cond_prob.T)
y_hat = self.labels[np.argmax(probs,axis=1)]
return y_hat