@@ -0,0 +1,1876 @@
+import math%0A%0Aclass NaiveBayesClassifier(object):%0A%09def __init__(self, x, y): %0A%09%09self.classes = set(y)%0A%09%09self.class_count = len(set(y))%0A%09%09self.train = zip(x,y)%0A%09%0A%09def classProb(self, clss, input, method='regular'):%0A%09%09try:%0A%09%09%09x_probab = %5B%5D%0A%09%09%09clssProbability = len(filter(lambda u: u%5B1%5D == clss, self.train))/float(len(self.train))%0A%09%09%09if method == 'regular':%0A%09%09%09%09clss_instances = filter(lambda u: u%5B1%5D == clss, self.train)%0A%09%09%09%09for i in range(len(input)):%0A%09%09%09%09%09x_occurrence = len(filter(lambda u: u%5B0%5D%5Bi%5D== input%5Bi%5D, clss_instances))%0A%09%09%09%09%09x_probab.append(x_occurrence/len(clss_instances))%09%0A%09%09%09if method == 'gaussian':%0A%09%09%09%09x_mean, x_stdev = self._classMean(clss), self._classStd(clss)%0A%09%09%09%09for i in range(len(input)):%0A%09%09%09%09%09x, mean, stdev = input%5Bi%5D, x_mean%5Bi%5D, x_stdev%5Bi%5D%0A%09%09%09%09%09if stdev == 0:%0A%09%09%09%09%09%09continue%0A%09%09%09%09%09exponent = math.exp(-(math.pow(x-mean,2)/float(2*math.pow(stdev,2))))%0A%09%09%09%09%09x_probab.append((1.0 / (math.sqrt(2*math.pi) * stdev)) * exponent)%0A%09%09%09return (reduce(lambda x,y: x*y, x_probab) * clssProbability)%0A%09%09except Exception as e:%0A%09%09%09print e%0A%09%0A%09def _classMean(self, clss):%0A%09%09def mean(values):%0A%09%09%09return sum(values)/len(values)%0A%09%09clss_instances = filter(lambda u: u%5B1%5D == clss, self.train)%0A%09%09X = %5Bu%5B0%5D for u in clss_instances%5D%0A%09%09x_mean = tuple(%5Bmean(attr) for attr in zip(*X)%5D)%0A%09%09return x_mean%0A%0A%09def _classStd(self, clss):%0A%09%09def stdev(values):%0A%09%09%09avg = sum(values)/len(values)%0A%09%09%09variance = sum(%5Bpow(x-avg,2) for x in values%5D)/float(len(values)-1)%0A%09%09%09return math.sqrt(variance)%0A%09%09clss_instances = filter(lambda u: u%5B1%5D == clss, self.train)%0A%09%09X = %5Bu%5B0%5D for u in clss_instances%5D%0A%09%09x_stdev = tuple(%5Bstdev(attr) for attr in zip(*X)%5D)%0A%09%09return x_stdev%0A%0A%09def predictClass(self, input, method='regular'):%0A%09%09probabilityMap = %7B%7D%0A%09%09for clss in self.classes:%0A%09%09%09probabilityMap%5Bclss%5D = classProb(clss, input, method)%0A%09%09print 'Prediction:', max(probabilityMap, key=probabilityMap.get)

Add Naive Bayes