This article is one of the realization of handwriting algorithm after I read Li Hang’s “Statistical Learning Methods”. Please refer to the fourth chapter of statistical learning methods naive Bayes method – Li Hang
The code is as follows:
# - * - coding: Utf-8 -*- # # Written by Tien Feng # email: [email protected] # written by: 26 April 2017 # Python version: 3.6.1 # CSDN: http://blog.csdn.net/fontthrone # # next behavior symbol calculate the package # import from fractions import Fraction class NaiveBayesMethod: The NaiveBayesMethod's internal calculation is now numerical, Symbolic computation code has comments, if you need to manually modify please Naive bayes classifier When lam = 1, class classification approach to implement the Laplacian smoothing for bayesian estimation, in order to avoid to compute probability is 0, so as to avoid the accumulation of calculation errors Specific principles refer to expericnce lam = > < statistical learning method chapter 4 Def __init__(self, inputArray, lam): self.input = inputArray self.lam = lam self.__lenInput = len(self.input) self.__y = self.input[self.__lenInput - 1] Self.__onlyy = self.__only(self.__y) self.__county = self.__countList(self.__onlyy __countList(self, list): count = {} for item in list: Count [item] = count. Get (item, 0) + 1 return len(count) # def __findy(self, list, y): result = True for i in range(0, len(list)): if list[i] == y: Def __only(self, list): onlyy = [] for I in range(0, len(list)): If self. __onlyy (list[I]): onlyy.append(list[I]) return onlyy # def __countKind(self, list, element): Return list.count(element) # def __findOnlyElement(self, list, x): Return self.__only(list).index(x) # def __py(self, x): # return Fraction(self.__countKind(self.__y, x) + self.lam, len(self.__y) + self.__county * self.lam) return (self.__countKind(self.__y, X) + self.lam)/(len(self.__y) + self.__county * self.lam) # def __realityx (self, list, x): # return Fraction(self.__countKind(list, x) + self.lam, len(list) + self.__countList(list) * self.lam) return (self.__countKind(list, x) + self.lam) / (len(list) + self.__countList(list) * self.lam) def __probabilityYX(self, list, x, yy): xx = self.__findOnlyElement(list, x) yindex = self.__findOnlyElement(self.__y, Yy) fz = 0 onlyx = self. __only onlyy = self. (list) __only (self. __y) # for p (y = | x1 =?) molecules for I in range (0, len (list)) : if list[i] == onlyx[xx] and self.__y[i] == onlyy[yindex]: fz += 1 # return Fraction(fz + self.lam, self.__countKind(list, onlyx[xx]) + self.__countList(list) * self.lam) return (fz + self.lam) / (self.__countKind(list, onlyx[xx]) + self.__countList(list) * self.lam) def fl(self, x, y): ps = [] for i in range(0, len(self.__onlyy)): p1 = self.__probabilityX(self.input[0], x) * self.__probabilityYX(self.input[0], x, 1) * self.__probabilityX( self.input[1], y) * self.__probabilityYX(self.input[1], y, Self.__onlyy [I])/self.__py(1) ps.append(p1) return self.__onlyy[ps.index(Max (ps))] # NaiveBayesMethod input = [[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3], [1, 2, 2, 1, 1, 1, 2, 2, 3, 3, 3, 2, 2, 3, 3], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1]] test = NaiveBayesMethod(input, 1) print(test.fl(2, 1)) test.lam = 0 print(test.fl(2, 1))Copy the code
The following output is displayed: - 1-1Copy the code