朴素贝叶斯法分类器的Python3 实现
生活随笔
收集整理的這篇文章主要介紹了
朴素贝叶斯法分类器的Python3 实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本篇文章是我在讀了李航的<統(tǒng)計(jì)學(xué)習(xí)方法>后手寫(xiě)的算法實(shí)現(xiàn)之一
原理請(qǐng)參考統(tǒng)計(jì)學(xué)習(xí)方法第四章樸素貝葉斯法-李航
代碼如下:
# - * - coding: utf - 8 -*- # # 作者:田豐 # 郵箱:fonttian@163.com # 撰寫(xiě)時(shí)間:2017年4月26日 # Python版本:3.6.1 # CSDN:http://blog.csdn.net/fontthrone # # 下一行為符號(hào)計(jì)算所導(dǎo)入的包 # from fractions import Fractionclass NaiveBayesMethod:'''NaiveBayesMethod 的內(nèi)部計(jì)算方式現(xiàn)在為數(shù)值計(jì)算,符號(hào)計(jì)算的代碼已經(jīng)注釋,如果需要請(qǐng)手動(dòng)修改樸素貝葉斯法分類器 當(dāng)lam=1 時(shí),類分類方式為為貝葉斯估計(jì)實(shí)現(xiàn)了拉普拉斯平滑,以此避免出現(xiàn)要計(jì)算的概率為0的情況,以免計(jì)算錯(cuò)誤的累積具體原理請(qǐng)參考李航的<統(tǒng)計(jì)學(xué)習(xí)方法>第四章lam = 0 時(shí) 類分類方式為為極大似然值估計(jì)'''def __init__(self, inputArray, lam):self.input = inputArrayself.lam = lamself.__lenInput = len(self.input)self.__y = self.input[self.__lenInput - 1]self.__onlyy = self.__only(self.__y)self.__county = self.__countList(self.__onlyy)# 計(jì)算列表總樣本數(shù) return intdef __countList(self, list):count = {}for item in list:count[item] = count.get(item, 0) + 1return len(count)# 檢查某列表中時(shí)候含有某個(gè)元素def __findy(self, list, y):result = Truefor i in range(0, len(list)):if list[i] == y:result = Falsereturn result# 返回列表種類def __only(self, list):onlyy = []for i in range(0, len(list)):if self.__findy(onlyy, list[i]):onlyy.append(list[i])return onlyy# 統(tǒng)計(jì)列表中某元素的個(gè)數(shù)def __countKind(self, list, element):return list.count(element)# 通過(guò)元素值返回位置索引def __findOnlyElement(self, list, x):return self.__only(list).index(x)# 先驗(yàn)概率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)# 返回p(x=?)def __probabilityX(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 = 0onlyx = self.__only(list)onlyy = self.__only(self.__y)# 獲取 p(y=?|x1=?) 的分子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))]# 測(cè)試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)) 輸出結(jié)果如下: -1 -1總結(jié)
以上是生活随笔為你收集整理的朴素贝叶斯法分类器的Python3 实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 统计学习方法第四章朴素贝叶斯法-李航
- 下一篇: 盘点selenium phantomJS