首页 > 代码库 > 朴素贝叶斯算法的python实现

朴素贝叶斯算法的python实现

朴素贝叶斯

算法优缺点

  • 优点:在数据较少的情况下依然有效,可以处理多类别问题

  • 缺点:对输入数据的准备方式敏感

  • 适用数据类型:标称型数据

算法思想:

朴素贝叶斯
比如我们想判断一个邮件是不是垃圾邮件,那么我们知道的是这个邮件中的词的分布,那么我们还要知道:垃圾邮件中某些词的出现是多少,就可以利用贝叶斯定理得到。
朴素贝叶斯分类器中的一个假设是:每个特征同等重要

函数

loadDataSet()

创建数据集,这里的数据集是已经拆分好的单词组成的句子,表示的是某论坛的用户评论,标签1表示这个是骂人的

createVocabList(dataSet)

找出这些句子中总共有多少单词,以确定我们词向量的大小

setOfWords2Vec(vocabList, inputSet)

将句子根据其中的单词转成向量,这里用的是伯努利模型,即只考虑这个单词是否存在

bagOfWords2VecMN(vocabList, inputSet)

这个是将句子转成向量的另一种模型,多项式模型,考虑某个词的出现次数

trainNB0(trainMatrix,trainCatergory)

计算P(i)和P(w[i]|C[1])和P(w[i]|C[0]),这里有两个技巧,一个是开始的分子分母没有全部初始化为0是为了防止其中一个的概率为0导致整体为0,另一个是后面乘用对数防止因为精度问题结果为0

classifyNB(vec2Classify, p0Vec, p1Vec, pClass1)

根据贝叶斯公式计算这个向量属于两个集合中哪个的概率高

  1.  1 #coding=utf-8 2 from numpy import * 3 def loadDataSet(): 4     postingList=[[my, dog, has, flea, problems, help, please], 5                  [maybe, not, take, him, to, dog, park, stupid], 6                  [my, dalmation, is, so, cute, I, love, him], 7                  [stop, posting, stupid, worthless, garbage], 8                  [mr, licks, ate, my, steak, how, to, stop, him], 9                  [quit, buying, worthless, dog, food, stupid]]10     classVec = [0,1,0,1,0,1]    #1 is abusive, 0 not11     return postingList,classVec12 13 #创建一个带有所有单词的列表14 def createVocabList(dataSet):15     vocabSet = set([])16     for document in dataSet:17         vocabSet = vocabSet | set(document)18     return list(vocabSet)19     20 def setOfWords2Vec(vocabList, inputSet):21     retVocabList = [0] * len(vocabList)22     for word in inputSet:23         if word in vocabList:24             retVocabList[vocabList.index(word)] = 125         else:26             print word ,word ,not in dict27     return retVocabList28 29 #另一种模型    30 def bagOfWords2VecMN(vocabList, inputSet):31     returnVec = [0]*len(vocabList)32     for word in inputSet:33         if word in vocabList:34             returnVec[vocabList.index(word)] += 135     return returnVec36 37 def trainNB0(trainMatrix,trainCatergory):38     numTrainDoc = len(trainMatrix)39     numWords = len(trainMatrix[0])40     pAbusive = sum(trainCatergory)/float(numTrainDoc)41     #防止多个概率的成绩当中的一个为042     p0Num = ones(numWords)43     p1Num = ones(numWords)44     p0Denom = 2.045     p1Denom = 2.046     for i in range(numTrainDoc):47         if trainCatergory[i] == 1:48             p1Num +=trainMatrix[i]49             p1Denom += sum(trainMatrix[i])50         else:51             p0Num +=trainMatrix[i]52             p0Denom += sum(trainMatrix[i])53     p1Vect = log(p1Num/p1Denom)#处于精度的考虑,否则很可能到限归零54     p0Vect = log(p0Num/p0Denom)55     return p0Vect,p1Vect,pAbusive56     57 def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):58     p1 = sum(vec2Classify * p1Vec) + log(pClass1)    #element-wise mult59     p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1)60     if p1 > p0:61         return 162     else: 63         return 064         65 def testingNB():66     listOPosts,listClasses = loadDataSet()67     myVocabList = createVocabList(listOPosts)68     trainMat=[]69     for postinDoc in listOPosts:70         trainMat.append(setOfWords2Vec(myVocabList, postinDoc))71     p0V,p1V,pAb = trainNB0(array(trainMat),array(listClasses))72     testEntry = [love, my, dalmation]73     thisDoc = array(setOfWords2Vec(myVocabList, testEntry))74     print testEntry,classified as: ,classifyNB(thisDoc,p0V,p1V,pAb)75     testEntry = [stupid, garbage]76     thisDoc = array(setOfWords2Vec(myVocabList, testEntry))77     print testEntry,classified as: ,classifyNB(thisDoc,p0V,p1V,pAb)78     79     80 def main():81     testingNB()82     83 if __name__ == __main__:84     main()

     

来自为知笔记(Wiz)



朴素贝叶斯算法的python实现