Skip to content

Commit

Permalink
Naive Bayes: Division by zero fix
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklindernoren committed Oct 4, 2017
1 parent b6ef93b commit 210620d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions mlfromscratch/examples/naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from mlfromscratch.supervised_learning import NaiveBayes

def main():
data = datasets.load_iris()
data = datasets.load_digits()
X = normalize(data.data)
y = data.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

clf = NaiveBayes()
clf.fit(X_train, y_train)
Expand Down
2 changes: 1 addition & 1 deletion mlfromscratch/supervised_learning/k_nearest_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy as np
from mlfromscratch.utils import euclidean_distance


class KNN():
""" K Nearest Neighbors classifier.
Expand Down Expand Up @@ -40,3 +39,4 @@ def predict(self, X_test, X_train, y_train):
label = self._vote(k_nearest_neighbors)
y_pred[i] = label
return y_pred

8 changes: 3 additions & 5 deletions mlfromscratch/supervised_learning/naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

class NaiveBayes():
"""The Gaussian Naive Bayes classifier. """
def __init__(self): pass

def fit(self, X, y):
self.X, self.y = X, y
self.classes = np.unique(y)
Expand All @@ -26,8 +24,9 @@ def fit(self, X, y):

def _calculate_likelihood(self, mean, var, x):
""" Gaussian likelihood of the data x given mean and var """
coeff = (1.0 / (math.sqrt((2.0 * math.pi) * var)))
exponent = math.exp(-(math.pow(x - mean, 2) / (2 * var)))
eps = 1e-4 # Add small term in denominator to avoid division by zero
coeff = 1.0 / (math.sqrt((2.0 * math.pi) * var) + eps)
exponent = math.exp(-(math.pow(x - mean, 2) / (2 * var + eps)))
return coeff * exponent

def _calculate_prior(self, c):
Expand Down Expand Up @@ -77,4 +76,3 @@ def predict(self, X):
y = self._classify(sample)
y_pred.append(y)
return y_pred

0 comments on commit 210620d

Please sign in to comment.