朴素贝叶斯算法的python实现

朴素贝叶斯算法的python实现,第1张

概述朴素贝叶斯算法的python实现

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#Coding=utf-8from numpy import *def loadDataSet():    postingList=[['my','dog','has','flea','problems','help','please'],['maybe','not','take','him','to','park','stupID'],['my','dalmation','is','so','cute','I','love','him'],['stop','posting','stupID','worthless','garbage'],['mr','licks','ate','my','steak','how','stop',['quit','buying','food','stupID']]    classvec = [0,1,1]    #1 is abusive,0 not    return postingList,classvec#创建一个带有所有单词的列表def createVocabList(dataSet):    vocabSet = set([])    for document in dataSet:        vocabSet = vocabSet | set(document)    return List(vocabSet)     def setofWords2Vec(vocabList,inputSet):    retVocabList = [0] * len(vocabList)    for word in inputSet:        if word in vocabList:            retVocabList[vocabList.index(word)] = 1        else:            print 'word ',word,'not in dict'    return retVocabList#另一种模型    def bagOfWords2VecMN(vocabList,inputSet):    returnVec = [0]*len(vocabList)    for word in inputSet:        if word in vocabList:            returnVec[vocabList.index(word)] += 1    return returnVecdef trainNB0(trainMatrix,trainCatergory):    numTrainDoc = len(trainMatrix)    numWords = len(trainMatrix[0])    pAbusive = sum(trainCatergory)/float(numTrainDoc)    #防止多个概率的成绩当中的一个为0    p0Num = ones(numWords)    p1Num = ones(numWords)    p0Denom = 2.0    p1Denom = 2.0    for i in range(numTrainDoc):        if trainCatergory[i] == 1:            p1Num +=trainMatrix[i]            p1Denom += sum(trainMatrix[i])        else:            p0Num +=trainMatrix[i]            p0Denom += sum(trainMatrix[i])    p1Vect = log(p1Num/p1Denom)#处于精度的考虑,否则很可能到限归零    p0Vect = log(p0Num/p0Denom)    return p0Vect,p1Vect,pAbusive     def classifyNB(vec2Classify,p0Vec,p1Vec,pClass1):    p1 = sum(vec2Classify * p1Vec) + log(pClass1)    #element-wise mult    p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1)    if p1 > p0:        return 1    else:         return 0         def testingNB():    ListOposts,ListClasses = loadDataSet()    myVocabList = createVocabList(ListOposts)    trainMat=[]    for postinDoc in ListOposts:        trainMat.append(setofWords2Vec(myVocabList,postinDoc))    p0V,p1V,pAb = trainNB0(array(trainMat),array(ListClasses))    testEntry = ['love','dalmation']    thisDoc = array(setofWords2Vec(myVocabList,testEntry))    print testEntry,'classifIEd as: ',classifyNB(thisDoc,p0V,pAb)    testEntry = ['stupID','garbage']    thisDoc = array(setofWords2Vec(myVocabList,pAb)          def main():    testingNB()     if __name__ == '__main__':    main()

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的朴素贝叶斯算法的python实现全部内容,希望文章能够帮你解决朴素贝叶斯算法的python实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199217.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存