如何理解naive Bayes
原理:
已知有兩個類class0,class1,已知文檔docx,現(xiàn)在需要判斷該文檔屬于哪一個類。
將其轉化為概率問題
即求P(class0|docx)和P(class1|docx)這兩個值,然后比大小
由bayes公式,P(class0|docx)P(docx) = P(docx|class0)*P(class0)。同理class1
P(class0|docx):docx屬于class0的概率
P(docx):產(chǎn)生出docx的概率
P(class0):表示class0的先驗概率,例如分類體育類,財經(jīng)類,任意拿了1萬個語料,體育類8000個,財經(jīng)類2000個,那么體育類先驗的就是80%。
P(docx|class0):表示class0生成出docx的概率
由于比大小P(docx)是完全一致的,因此略去
則計算的是P(docx|class0)*P(class0),其中P(class0)可以從語料中觀測到。
現(xiàn)在就歸結到計算P(docx|class0)
在詞袋模型的假設下,docx可以由獨立同分布的詞向量構成,即docx={term1,term2,term3...termn}。
即P(docx|class0) = P( {x1,x2,x3,..xn} |class0),其中x1表示term1出現(xiàn)的次數(shù),第一維表示term1
由獨立假設
?P( {x1,x2,x3,..xn} |class0)=P({x1,0,0,...0}|class0)*P({0,x2,0,0,...}|class0)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? =(y1^x1)*(y2^x2)... ? ?y1表示term1在class0的概率。即class0的全部文檔看作是1個文檔,計算term1的出現(xiàn)概率。
兩邊取對數(shù)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?=x1log(y1)+x2log(y2)....
如果對整個P(docx|class0)*P(class0)取對數(shù),在求負數(shù),相當用用f(x) = -log(x)作用一下x,則有
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -log(P(docx|class0)*P(class0)) = -log(P(class0)) + - x1log(y1)+ -x2log(y2).... ?其中x1表示在docx中term1出現(xiàn)的次數(shù)
至此我們可以將公式看作一個docx對class0提供的信息量的累加,在什么信息都沒有的情況下,是class0的信息量是 -log(P(class0),在term1出現(xiàn)了x1次以后,信息量增加了-x1log(y1),依次往復,相當于docx用class0這個model能壓縮的最小物理空間XXbit。也就是說,借助class0的分布,只用XXbit就可以表示docx。從壓縮上想不難理解,class0提供了一個特征的概率分布特點,用香濃公式來對每個特征按照概率編碼。
?? ? ? ? ? ?
?舉個計算過程的例子
1)訓練語料如下
?? ? ? ?term1 ? term2 ?term3 ?term4 ? class
doc1 ? ?0 ? ? ? 1 ? ? ? ? ? 1 ? ? ? ? ? 1 ? ? ? ? 1 ??doc2 ? ?1 ? ? ? 1 ? ? ? ? ? 0 ? ? ? ? ? 0 ? ? ? ? 0?
doc3 ? ?2 ? ? ? 3 ? ? ? ? ? 1 ? ? ? ? ? 0 ? ? ? ? 0 ? ?
doc4 ? ?2 ? ? ? 2 ? ? ? ? ? 0 ? ? ? ? ? 1 ? ? ? ? 0 ??
doc5 ? ?1 ? ? ? 0 ? ? ? ? ? 2 ? ? ? ? ? 3 ? ? ? ? 1
2)計算model parameter
w0(1) = log(2/5) ? w0(0) = log(3/5) ? ? ?[在5個樣本中,label 1有2個,lable 0有3個]
w1(1) = log(1/9) ? w1(0) = log(5/13) ? ?[term1 在label為0的標簽的3個樣本中,出現(xiàn)了5次,label為0的標簽的3個樣本中共計各種term出現(xiàn)了13次]
w2(1) = log(1/9) ? w2(0) = log(6/13) ? ?
w3(1) = log(3/9) ? w3(0) = log(1/13)
w4(1) = log(4/9) ? w4(0) = log(1/13)
3)given test sample
docx (1,3,2,0,1) ?{1表示我是一個文檔,3表示term1出現(xiàn)了3次,2表示term2出現(xiàn)了2次...}4)計算屬于哪個類
p(1|docx) = -(1*log(2/5)+3*log(1/9)+2*log(1/9)+0*log(3/9)+1*log(4/9)) = ?6.4755775834193
p(0|docx) = -(1*log(3/5)+3*log(5/13)+2*log(6/13)+0*log(1/13)+1*log(1/13) )= ?3.6672696976529so docx is class 0,用class0的分布表示docx,只需要3.66比特,docx和class0更具有分布上的一致性。
5)理解
w0(0),w1(0)表示docx一個term都不含的時候,直接用類上的比例來推測可能得分類。比如某個docx為空文檔。wi(0)的物理含義 ?tf向量,表示class0生成termi的能力。
wi(1)的物理含義 ?tf向量,表示class1生成termi的能力。
w(0)可以看作是一個指向label為0的samples的向量。
w(1)可以看做是一個指向label為1的samples的向量。
docx*w(0) > docx*w(1) 表示docx的類為0
docx*w(1) > docx*w(0) 表示docx的類是1
docx*w(0) = docx*w(0) 表示docx恰好距離類0和類1相同。也可理解為夾角相同,如果同除以docx和w的模就可以理解為夾角。
6)最后訓練出的model是什么?
就是訓練語料每個term出現(xiàn)多少次,而總次數(shù)可以將這些數(shù)字加和得到,不需存儲。
因此
class 0的model就是
?? ?term1 5
?? ?term2 6
?? ?term3 1
?? ?term4 1
在加上每個class的數(shù)目。
完畢,就是一個數(shù)數(shù)過程。
from:?http://blog.csdn.net/pennyliang/article/details/6651897
總結
以上是生活随笔為你收集整理的如何理解naive Bayes的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BP反向传播算法浅谈(Error Bac
- 下一篇: 【Machine Learning实验1