Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update naive_bayes.py #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions mlfromscratch/supervised_learning/naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,36 @@ def predict(self, X):
""" Predict the class labels of the samples in X """
y_pred = [self._classify(sample) for sample in X]
return y_pred

class BernoulliNaiveBayes:
def __init__(self,X,y):
self.X = X
self.y = y
self.N, self.D = X.shape
self.classes = np.unique(y)
self.C = len(self.classes)
self.prior = np.zeros(self.C,dtype=np.float64)
self.likelihood = np.zeros((self.C,self.D),dtype=np.float64)
self.N1 = sum(self.y)

def fit(self,X,y): # Does the fit by calculating the prior and the likelihood of the training data
for c in self.classes:
X_c = X[c == y]
X_c_sum = np.sum(X_c, axis=0)
self.prior[c] = X_c.shape[0] / float(self.N)
for i in range(len(self.likelihood[0])):
self.likelihood[c,i] = X_c_sum[i]/len(X_c)

def predict(self,X_test): #Gives the predicted labels for dataset
y_pred = [self._predict(x) for x in X_test]
return y_pred

def _predict(self, x):
posteriors = []
for i, c in enumerate(self.classes):
log_prior = np.log1p(self.prior[i])
log_likelihood = np.sum(np.log1p(self.likelihood[i]*x)) + np.sum(np.log1p(self.likelihood[i]*(1-x)))
posterior = log_prior + log_likelihood
posteriors.append(posterior)
return self.classes[np.argmax(posteriors)]