计算 P ( X ∣ C i ) P(X|C_i) P(X∣Ci),朴素贝叶斯分类假设类条件独立,即给定样本属性值相互条件独立。 P ( x 1 , … , x k ∣ C i ) = P ( x 1 ∣ C i ) ⋅ … ⋅ P ( x k ∣ C i ) P(x_1,…,x_k|C_i) = P(x_1|C_i)·…·P(x_k|C_i) P(x1,…,xk∣Ci)=P(x1∣Ci)⋅…⋅P(xk∣Ci)
贝叶斯定理: P ( C i ∣ X ) = P ( X ∣ C i ) ⋅ P ( C i ) P ( X ) = P ( X ∣ C i ) ⋅ P ( C i ) ∑ j = 1 c P ( X ∣ C j ) ⋅ P ( C j ) P(C_i|X)= \frac {P(X|C_i) \ \cdot \ P(C_i)} {P(X)} = \frac {P(X|C_i) \ \cdot \ P(C_i)} {\sum_{j=1}^{c}P(X|C_j) \ \cdot \ P(C_j) } P(Ci∣X)=P(X)P(X∣Ci) ⋅ P(Ci)=∑j=1cP(X∣Cj) ⋅ P(Cj)P(X∣Ci) ⋅ P(Ci)
i i i 表示 l a b e l label label 的类别数
j j j 也表示 l a b e l label label 的类别数,只是为了区别于 i i i
先验概率 p r i o r p r o b a b i l i t y prior \ probability prior probability: P ( C i ) P(C_i) P(Ci),在事情发生之前的概率。根据以往经验和分析得到的概率!eg,比如抛硬币,我们都认为正面朝上的概率是 0.5!
概率密度函数 p r o b a b i l i t y d e n s i t y f u n c t i o n probability \ density \ function probability density function: P ( X ∣ C i ) P(X|C_i) P(X∣Ci)
后验概率 p o s t e r i o r p r o b a b i l i t i e s posterior \ probabilities posterior probabilities: P ( C i ∣ X ) P(C_i|X) P(Ci∣X),事情发生了,可能有很多原因,判断事情发生时由哪个原因引起的概率!
总结,根据先验概率和概率密度函数,计算后验概率
eg: 对于一个二分类问题, y e s o r n o yes \ or \ no yes or no, 对应的贝叶斯公式如下
P ( Y e s ∣ X ) = P ( X ∣ Y e s ) ⋅ P ( Y e s ) P ( X ) = P ( X ∣ Y e s ) ⋅ P ( Y e s ) P ( X ∣ Y e s ) ⋅ P ( Y e s ) + P ( X ∣ N o ) ⋅ P ( N o ) P(Yes|X)= \frac {P(X|Yes) \cdot P(Yes)} {P(X)} = \frac {P(X|Yes) \ \cdot \ P(Yes)} {P(X|Yes) \cdot P(Yes) + P(X|No) \cdot P(No)} P(Yes∣X)=P(X)P(X∣Yes)⋅P(Yes)=P(X∣Yes)⋅P(Yes)+P(X∣No)⋅P(No)P(X∣Yes) ⋅ P(Yes)
P ( N o ∣ X ) = P ( X ∣ N o ) ⋅ P ( N o ) P ( X ) = P ( X ∣ N o ) ⋅ P ( N o ) P ( X ∣ Y e s ) ⋅ P ( Y e s ) + P ( X ∣ N o ) ⋅ P ( N o ) P(No|X)= \frac {P(X|No) \cdot P(No)} {P(X)} = \frac {P(X|No) \ \cdot \ P(No)} {P(X|Yes) \cdot P(Yes) + P(X|No) \cdot P(No)} P(No∣X)=P(X)P(X∣No)⋅P(No)=P(X∣Yes)⋅P(Yes)+P(X∣No)⋅P(No)P(X∣No) ⋅ P(No)
如果 P ( Y e s ∣ X ) > P ( N o ∣ X ) P(Yes|X)>P(No|X) P(Yes∣X)>P(No∣X),分类结果为 Y e s Yes Yes,反之结果为 N o No No
对 X = { G e n d e r = F e m a l e , I n c o m e = H i g h , A g e = M i d d l e } X = \left \{ Gender = Female,Income = High, Age = Middle \right \} X={Gender=Female,Income=High,Age=Middle} 计算分类结果 Y e s o r N o Yes \ or \ No Yes or No
P ( Y e s ) = 3 / 6 P(Yes) = 3 / 6 P(Yes)=3/6
由图知
P ( G e n d e r = F e m a l e ∣ Y e s ) = 2 / 3 P\left (Gender = Female \mid Yes\right ) = 2/3 P(Gender=Female∣Yes)=2/3
P ( I n c o m e = H i g h ∣ Y e s ) = 3 / 3 P\left ( Income = High \mid Yes\right ) = 3/3 P(Income=High∣Yes)=3/3
P ( A g e = M i d d l e ∣ Y e s ) = 1 / 3 P\left ( Age = Middle \mid Yes\right ) = 1/3 P(Age=Middle∣Yes)=1/3
所以
P ( X ∣ Y e s ) ⋅ P ( Y e s ) = P ( G e n d e r = F e m a l e ∣ Y e s ) ⋅ P ( I n c o m e = H i g h ∣ Y e s ) ⋅ P ( A g e = M i d d l e ∣ Y e s ) ⋅ P ( Y e s ) = 2 3 × 3 3 × 1 3 × 3 6 ≈ 0.111 P\left ( X\mid Yes \right ) \cdot P(Yes)= P\left (Gender = Female \mid Yes\right )\cdot P\left ( Income = High \mid Yes\right )\cdot P\left ( Age = Middle \mid Yes\right ) \cdot P(Yes) = \frac{2}{3}\times\frac{3}{3}\times\frac{1}{3}\times\frac{3}{6} \approx 0.111 P(X∣Yes)⋅P(Yes)=P(Gender=Female∣Yes)⋅P(Income=High∣Yes)⋅P(Age=Middle∣Yes)⋅P(Yes)=32×33×31×63≈0.111
P ( N o ) = 3 / 6 P(No) = 3/6 P(No)=3/6
由图知
P ( G e n d e r = F e m a l e ∣ N o ) = 1 / 3 P\left (Gender = Female \mid No\right ) = 1/3 P(Gender=Female∣No)=1/3
P ( I n c o m e = H i g h ∣ N o ) = 1 / 3 P\left ( Income = High \mid No\right ) = 1/3 P(Income=High∣No)=1/3
P ( A g e = M i d d l e ∣ N o ) = 2 / 3 P\left ( Age = Middle \mid No\right ) = 2/3 P(Age=Middle∣No)=2/3
所以
P ( X ∣ N o ) ⋅ P ( N o ) = P ( G e n d e r = F e m a l e ∣ N o ) ⋅ P ( I n c o m e = H i g h ∣ N o ) ⋅ P ( A g e = M i d d l e ∣ N o ) ⋅ P ( N o ) = 1 3 × 1 3 × 2 3 × 3 6 = 0.037 P\left ( X\mid No \right ) \cdot P(No)= P\left (Gender = Female \mid No\right )\cdot P\left ( Income = High \mid No\right )\cdot P\left ( Age = Middle \mid No\right ) \cdot P(No) = \frac{1}{3}\times\frac{1}{3}\times\frac{2}{3}\times\frac{3}{6} = 0.037 P(X∣No)⋅P(No)=P(Gender=Female∣No)⋅P(Income=High∣No)⋅P(Age=Middle∣No)⋅P(No)=31×31×32×63=0.037
P ( Y e s ∣ X ) = P ( X ∣ Y e s ) ⋅ P ( Y e s ) P ( X ∣ Y e s ) ⋅ P ( Y e s ) + P ( X ∣ N o ) ⋅ P ( N o ) = 0.111 0.111 + 0.037 = 75 % P(Yes|X)= \frac {P(X|Yes) \ \cdot \ P(Yes)} {P(X|Yes) \cdot P(Yes) + P(X|No) \cdot P(No)} = \frac{0.111}{0.111+0.037} = 75 \% P(Yes∣X)=P(X∣Yes)⋅P(Yes)+P(X∣No)⋅P(No)P(X∣Yes) ⋅ P(Yes)=0.111+0.0370.111=75%
P ( N o ∣ X ) = P ( X ∣ N o ) ⋅ P ( N o ) P ( X ∣ Y e s ) ⋅ P ( Y e s ) + P ( X ∣ N o ) ⋅ P ( N o ) = 0.037 0.111 + 0.037 = 25 % P(No|X)= \frac {P(X|No) \ \cdot \ P(No)} {P(X|Yes) \cdot P(Yes) + P(X|No) \cdot P(No)} = \frac{0.037}{0.111+0.037} = 25 \% P(No∣X)=P(X∣Yes)⋅P(Yes)+P(X∣No)⋅P(No)P(X∣No) ⋅ P(No)=0.111+0.0370.037=25%
因为
P ( Y e s ∣ X ) > P ( N o ∣ X ) P(Yes|X)>P(No|X) P(Yes∣X)>P(No∣X)
所以
分类结果为 Y e s Yes Yes
为什么后验概率要利用Bayes公式从先验概率和类条件概率密度函数计算获得。这是因为计算概率都要拥有大量数据才行。在估计先验概率与类条件概率密度函数时都可搜集到大量样本,而对某一特定事件(如x)要搜集大量样本是不太容易 的。因此只能借助Bayes公式来计算得到。
对基于最小错误率的贝叶斯决策来说,以后验概率值的大小作判据是最基本的方法,而其它形式的作用(如下)都基本相同,但使用时更方便些。
(1)
如果 P ( w i ∣ x ) = max j = 1 , 2 P ( w j ∣ x ) P(w_i|x)= \max_{j=1,2}P(w_j|x) P(wi∣x)=j=1,2maxP(wj∣x) 则 x ∈ w i x\in w_i x∈wi
(2)
如果 P ( x ∣ w i ) P ( w i ) = max j = 1 , 2 P ( x ∣ w j ) P ( w i ) P(x|w_i)P(w_i)= \max_{j=1,2}P(x|w_j)P(w_i) P(x∣wi)P(wi)=j=1,2maxP(x∣wj)P(wi)
则 x ∈ w i x \in w_i x∈wi
(3)似然比
如果 l ( x ) = p ( x ∣ w 1 ) p ( x ∣ w 2 ) > p ( w 2 ) p ( w 1 ) l(x) = \frac{p(x|w_1)}{p(x|w_2)} > \frac{p(w_2)}{p(w_1)} l(x)=p(x∣w2)p(x∣w1)>p(w1)p(w2)
则 x ∈ w 1 x \in w_1 x∈w1 否则 x ∈ w 2 x \in w_2 x∈w2
(4) 似然比的负对数 -ln
P ( e ) = ∫ R 1 P ( x ∣ w 2 ) P ( w 2 ) d x + ∫ R 2 P ( x ∣ w 1 ) P ( w 1 ) d x P(e) = \int_{R_1}P(x|w_2)P(w_2)dx + \int_{R_2}P(x|w_1)P(w_1)dx P(e)=∫R1P(x∣w2)P(w2)dx+∫R2P(x∣w1)P(w1)dx
如下图所示
阴影处就是 P ( e ) P(e) P(e) ,也可以写成
P ( e ) = ∫ R 1 P ( w 2 ∣ x ) P ( x ) d x + ∫ R 2 P ( w 1 ∣ x ) P ( x ) d x P(e) = \int_{R_1}P(w_2|x)P(x)dx + \int_{R_2}P(w_1|x)P(x)dx P(e)=∫R1P(w2∣x)P(x)dx+∫R2P(w1∣x)P(x)dx
加了权重
在决策中,除了关心决策的正确与否,有时我们更关心错误的决策将带来的损失。比如在判断细胞是否为癌细胞的决策中,若把正常细胞判定为癌细胞,将会增加患者的负担和不必要的治疗,但若把癌细胞判定为正常细胞,将会导致患者失去宝贵的发现和治疗癌症的机会,甚至会影响患者的生命。这两种类型的决策错误所产生的代价是不同的。
考虑各种错误造成损失不同时的一种最优决策,就是所谓的最小风险贝叶斯决策。设对于实际状态为wj 的向量xx采取决策αi 所带来的损失为
该函数称为损失函数,通常它可以用表格的形式给出,叫做决策表。需要知道,最小风险贝叶斯决策中的决策表是需要人为确定的,决策表不同会导致决策结果的不同,因此在实际应用中,需要认真分析所研究问题的内在特点和分类目的,与应用领域的专家共同设计出适当的决策表,才能保证模式识别发挥有效的作用。 对于一个实际问题,对于样本xx,最小风险贝叶斯决策的计算步骤如下: (1)利用贝叶斯公式计算后验概率:
其中要求先验概率和类条件概率已知。 (2)利用决策表,计算条件风险:
(3)决策:选择风险最小的决策,即:
现在用之前的判别细胞是否为癌细胞为例。状态1为正常细胞,状态2为癌细胞,假设:
参考 最小风险贝叶斯决策