# Import the required modules
%pylab inline
import numpy as np
Populating the interactive namespace from numpy and matplotlib
'''
Class for performing classification using Gaussian Naive Bayes.
'''
class naive_bayes():
# Get the observed data and the class labels and
# calculate the mean and variance for each class label
def __init__(self, x, y):
self.mean_0 = np.mean([x_i for x_i, y_i in zip(x, y) if y_i == 0])
self.var_0 = np.std([x_i for x_i, y_i in zip(x, y) if y_i == 0])**2
self.mean_1 = np.mean([x_i for x_i, y_i in zip(x, y) if y_i == 1])
self.var_1 = np.std([x_i for x_i, y_i in zip(x, y) if y_i == 1])**2
# The prior probabilities
def prior(self, prior):
self.priori = prior
# Likelihood of observing some data
def likelihood(self, x, true=True):
if true:
return 1 / sqrt(2.*np.pi*self.var_1) * np.exp(-(x-self.mean_1)**2/(2.*self.var_1))
else:
return 1 / sqrt(2.*np.pi*self.var_0) * np.exp(-(x-self.mean_0)**2/(2.*self.var_0))
# Posterior Probabilities
def posterior(self, x):
p_0 = self.priori[0]*self.likelihood(x, False)
p_1 = self.priori[1]*self.likelihood(x, True)
# print "Probability of True = {}".format(p_0)
# print "Probability of False = {}".format(p_1)
if p_0 > p_1:
return 0
else:
return 1
# x is list of 1:50
x = range(50)
# y is list
y = zeros(50)
y[25:] = 1
print
nb = naive_bayes(x, y)
nb.prior([1/2., 1/2.])
t = map(nb.posterior, x)
print "{} of {} correctly classified".format(sum(t == y), len(y))
50 of 50 correctly classified