forked from rushter/MLAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_bayes.py
57 lines (43 loc) · 1.83 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
import numpy as np
from mla.base import BaseEstimator
from mla.neuralnet.activations import softmax
class NaiveBayesClassifier(BaseEstimator):
"""Gaussian Naive Bayes."""
# Binary problem.
n_classes = 2
def fit(self, X, y=None):
self._setup_input(X, y)
# Check target labels
assert list(np.unique(y)) == [0, 1]
# Mean and variance for each class and feature combination
self._mean = np.zeros((self.n_classes, self.n_features), dtype=np.float64)
self._var = np.zeros((self.n_classes, self.n_features), dtype=np.float64)
self._priors = np.zeros(self.n_classes, dtype=np.float64)
for c in range(self.n_classes):
# Filter features by class
X_c = X[y == c]
# Calculate mean, variance, prior for each class
self._mean[c, :] = X_c.mean(axis=0)
self._var[c, :] = X_c.var(axis=0)
self._priors[c] = X_c.shape[0] / float(X.shape[0])
def _predict(self, X=None):
# Apply _predict_proba for each row
predictions = np.apply_along_axis(self._predict_row, 1, X)
# Normalize probabilities so that each row will sum up to 1.0
return softmax(predictions)
def _predict_row(self, x):
"""Predict log likelihood for given row."""
output = []
for y in range(self.n_classes):
prior = np.log(self._priors[y])
posterior = np.log(self._pdf(y, x)).sum()
prediction = prior + posterior
output.append(prediction)
return output
def _pdf(self, n_class, x):
"""Calculate Gaussian PDF for each feature."""
mean = self._mean[n_class]
var = self._var[n_class]
numerator = np.exp(-(x - mean) ** 2 / (2 * var))
denominator = np.sqrt(2 * np.pi * var)
return numerator / denominator