Skip to content

Commit dba1ef0

Browse files
Naive Bayes: Division by zero fix
1 parent 210620d commit dba1ef0

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

mlfromscratch/supervised_learning/naive_bayes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ def fit(self, X, y):
2525
def _calculate_likelihood(self, mean, var, x):
2626
""" Gaussian likelihood of the data x given mean and var """
2727
eps = 1e-4 # Add small term in denominator to avoid division by zero
28-
coeff = 1.0 / (math.sqrt((2.0 * math.pi) * var) + eps)
28+
coeff = 1.0 / (math.sqrt((2.0 * math.pi) * var + eps))
2929
exponent = math.exp(-(math.pow(x - mean, 2) / (2 * var + eps)))
3030
return coeff * exponent
3131

3232
def _calculate_prior(self, c):
3333
""" Calculate the prior of class c
3434
(samples where class == c / total number of samples)"""
35-
# Selects the rows where the class label is c
3635
X_where_c = self.X[np.where(self.y == c)]
3736
n_class_instances = X_where_c.shape[0]
3837
n_total_instances = self.X.shape[0]

0 commit comments

Comments
 (0)