樸素貝葉斯算法優(yōu)缺點(diǎn)
優(yōu)點(diǎn):在數(shù)據(jù)較少的情況下依然有效,可以處理多類別問題
缺點(diǎn):對輸入數(shù)據(jù)的準(zhǔn)備方式敏感
適用數(shù)據(jù)類型:標(biāo)稱型數(shù)據(jù)
算法思想:
比如我們想判斷一個郵件是不是垃圾郵件,那么我們知道的是這個郵件中的詞的分布,那么我們還要知道:垃圾郵件中某些詞的出現(xiàn)是多少,就可以利用貝葉斯定理得到。
樸素貝葉斯分類器中的一個假設(shè)是:每個特征同等重要
函數(shù)
loadDataSet()
創(chuàng)建數(shù)據(jù)集,這里的數(shù)據(jù)集是已經(jīng)拆分好的單詞組成的句子,表示的是某論壇的用戶評論,標(biāo)簽1表示這個是罵人的
createVocabList(dataSet)
找出這些句子中總共有多少單詞,以確定我們詞向量的大小
setOfWords2Vec(vocabList, inputSet)
將句子根據(jù)其中的單詞轉(zhuǎn)成向量,這里用的是伯努利模型,即只考慮這個單詞是否存在
bagOfWords2VecMN(vocabList, inputSet)
這個是將句子轉(zhuǎn)成向量的另一種模型,多項式模型,考慮某個詞的出現(xiàn)次數(shù)
trainNB0(trainMatrix,trainCatergory)
計算P(i)和P(w[i]|C[1])和P(w[i]|C[0]),這里有兩個技巧,一個是開始的分子分母沒有全部初始化為0是為了防止其中一個的概率為0導(dǎo)致整體為0,另一個是后面乘用對數(shù)防止因為精度問題結(jié)果為0
classifyNB(vec2Classify, p0Vec, p1Vec, pClass1)
根據(jù)貝葉斯公式計算這個向量屬于兩個集合中哪個的概率高
代碼如下:
#coding=utf-8
from numpy import *
def loadDataSet():
postingList=[['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
['stop', 'posting', 'stupid', 'worthless', 'garbage'],
['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'],
['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]
classVec = [0,1,0,1,0,1] #1 is abusive, 0 not
return postingList,classVec
#創(chuàng)建一個帶有所有單詞的列表
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 returnVec
def trainNB0(trainMatrix,trainCatergory):
numTrainDoc = len(trainMatrix)
numWords = len(trainMatrix[0])
pAbusive = sum(trainCatergory)/float(numTrainDoc)
#防止多個概率的成績當(dāng)中的一個為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', 'my', 'dalmation']
thisDoc = array(setOfWords2Vec(myVocabList, testEntry))
print testEntry,'classified as: ',classifyNB(thisDoc,p0V,p1V,pAb)
testEntry = ['stupid', 'garbage']
thisDoc = array(setOfWords2Vec(myVocabList, testEntry))
print testEntry,'classified as: ',classifyNB(thisDoc,p0V,p1V,pAb)
def main():
testingNB()
if __name__ == '__main__':
main()
希望本文所述對大家的Python程序設(shè)計有所幫助。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com