# naivebayes.py - # -*- coding: utf-8 -*import numpy as np...

• 2

This preview shows page 1 - 2 out of 2 pages.

# -*- coding: utf-8 -*-import numpy as npclass NaiveBayes:def predict(self, X):y_pred = [self._predict(x) for x in X]return np.array(y_pred)def _predict(self, x):probability_posteriors = []# calculate posterior probability for each classfor idx, c in enumerate(self._classes):prior = np.log(self._priors[idx])posterior = np.sum(np.log(self._pdf(idx, x)))posterior = prior + posteriorprobability_posteriors.append(posterior)# return class with highest posterior probabilityreturn self._classes[np.argmax(probability_posteriors)]def fit(self, X, y):n_samples, n_features = X.shapeself._classes = np.unique(y)n_classes = len(self._classes)# calculate mean, var, and prior for each classself._mean = np.zeros((n_classes, n_features), dtype=np.float64)self._var = np.zeros((n_classes, n_features), dtype=np.float64)self._priors =np.zeros(n_classes, dtype=np.float64)for idx, c in enumerate(self._classes):X_c = X[y==c]self._mean[idx, :] = X_c.mean(axis=0)self._var[idx, :] = X_c.var(axis=0)self._priors[idx] = X_c.shape / float(n_samples)def _pdf(self, class_idx, x):mean = self._mean[class_idx]var = self._var[class_idx]numerator = np.exp(- (x-mean)**2 / (2 * var))
End of preview. Want to read all 2 pages?

Course Hero member to access this document

Term
Spring
Professor
Sarim baig
Tags
Probability theory, Mild
• • • 