朴素贝叶斯分类器

朴素贝叶斯分类器(Naive Bayes Classifier)

基本概念

贝叶斯分类器是一族分类算法的总称,该族算法均以贝叶斯定理为基础,统称为贝叶斯分类器。贝叶斯分类器的分类原理是通过先验概率利用贝叶斯公式计算出其后验概率,选择具有最大后验概率的类作为该对象所属的类别。
S S 为实验E的样本空间, B1,B2,,Bn B 1 , B 2 , … , B n E E 的一组事件,若:
1. BiBj=,ij,i,j=1,2,,n
2. B1B2Bn=S B 1 ∪ B 2 ∪ ⋯ ∪ B n = S

则称 B1,B2,,Bn B 1 , B 2 , … , B n 为样本空间 S S 的一个划分。对于每次试验,事件B1,B2,,Bn中有且仅有一个事件发生。
A A 为实验E的事件,且 P(A)>0,P(Bi)0 P ( A ) > 0 , P ( B i ) ≥ 0 ,则:

  • 全概率公式:
    P(A)=P(A/B1)P(B1)+P(A/B2)P(B2)++P(A/Bn)P(Bn)=i=1nP(A/Bi)P(Bi)(1) (1) P ( A ) = P ( A / B 1 ) ∗ P ( B 1 ) + P ( A / B 2 ) ∗ P ( B 2 ) + ⋯ + P ( A / B n ) ∗ P ( B n ) = ∑ i = 1 n P ( A / B i ) P ( B i )
  • 贝叶斯定理:
    P(Bj/A)=P(A/Bj)P(Bj)ni=1P(A/Bi)P(Bi)(2) (2) P ( B j / A ) = P ( A / B j ) P ( B j ) ∑ i = 1 n P ( A / B i ) P ( B i )

假设有训练集 D D ,样本x=(x1,x2,,xn),类别标签 y=c1,c2,,ck y = c 1 , c 2 , … , c k 。对于某一样本 xi x i ,假设其类别标签为 c c ,则朴素贝叶斯分类器的训练过程就是基于D来估计类别先验概率 P(c) P ( c ) ,并为每个属性估计条件概率 P(xi|c) P ( x i | c )
朴素贝叶斯分类器(naive Bayes classifier)采用了“属性条件独立性假设”:对已知类别,假设所有的属性相互独立,也就是说,假设每个属性独立地对分类结果产生影响。

举例说明

下面以图1的示例来说明朴素贝叶斯分类器的决策过程:

ellipse
图1

已知训练数据如图1所示,那么给定一样本 {Credit=excellent,Term=5yrs,Income=high} { C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h } ,它的类别标签 y y 应该是多少呢?
对于该问题用朴素贝叶斯进行分类,我们只需要计算P(y=safe|Credit=excellent,Term=5yrs,Income=high) P(y=risky|Credit=excellent,Term=5yrs,Income=high) P ( y = r i s k y | C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) 并计算它们的大小即可,如果前者大则该样本的类别标签为 safe s a f e ,如果后者大则该样本的类别标签为 risky r i s k y
由图1我们可得到一些先验概率:

P(y=safe)=59P(y=risky)=49P(Credit=excellent|y=safe)=19P(Term=5yrs|y=safe)=29P(Income=high|y=safe)=39P(Credit=excellent|y=risky)=19P(Term=5yrs|y=risky)=29P(Income=high|y=risky)=29P(Credit=excellent)=29P(Term=5yrs)=49P(Income=high)=59 { P ( y = s a f e ) = 5 9 P ( y = r i s k y ) = 4 9 P ( C r e d i t = e x c e l l e n t | y = s a f e ) = 1 9 P ( T e r m = 5 y r s | y = s a f e ) = 2 9 P ( I n c o m e = h i g h | y = s a f e ) = 3 9 P ( C r e d i t = e x c e l l e n t | y = r i s k y ) = 1 9 P ( T e r m = 5 y r s | y = r i s k y ) = 2 9 P ( I n c o m e = h i g h | y = r i s k y ) = 2 9 P ( C r e d i t = e x c e l l e n t ) = 2 9 P ( T e r m = 5 y r s ) = 4 9 P ( I n c o m e = h i g h ) = 5 9

由公式(2)可知:
P(y=safe|Credit=excellent,Term=5yrs,Income=high)=P(Credit=excellent,Term=5yrs,Income=high|y=safe)P(y=safe)P(Credit=excellent,Term=5yrs,Income=high)P(y=risky|Credit=excellent,Term=5yrs,Income=high)=P(Credit=excellent,Term=5yrs,Income=high|y=risky)P(y=risky)P(Credit=excellent,Term=5yrs,Income=high) P ( y = s a f e | C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) = P ( C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h | y = s a f e ) P ( y = s a f e ) P ( C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) P ( y = r i s k y | C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) = P ( C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h | y = r i s k y ) P ( y = r i s k y ) P ( C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h )


P(Credit=excellent,Term=5yrs,Income=high|y=safe)=P(Credit=excellent|y=safe)P(Term=5yrs|y=safe)P(Income=high|y=safe)=192939=6729P(Credit=excellent,Term=5yrs,Income=high|y=risky)=P(Credit=excellent|y=risky)P(Term=5yrs|y=risky)P(Income=high|y=risky)=192929=4729P(Credit=excellent,Term=5yrs,Income=high)=P(Credit=excellent)P(Term=5yrs)P(Income=high)=294959=40729 P ( C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h | y = s a f e ) = P ( C r e d i t = e x c e l l e n t | y = s a f e ) ∗ P ( T e r m = 5 y r s | y = s a f e ) ∗ P ( I n c o m e = h i g h | y = s a f e ) = 1 9 ∗ 2 9 ∗ 3 9 = 6 729 P ( C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h | y = r i s k y ) = P ( C r e d i t = e x c e l l e n t | y = r i s k y ) ∗ P ( T e r m = 5 y r s | y = r i s k y ) ∗ P ( I n c o m e = h i g h | y = r i s k y ) = 1 9 ∗ 2 9 ∗ 2 9 = 4 729 P ( C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) = P ( C r e d i t = e x c e l l e n t ) ∗ P ( T e r m = 5 y r s ) ∗ P ( I n c o m e = h i g h ) = 2 9 ∗ 4 9 ∗ 5 9 = 40 729

所以
P(y=safe|Credit=excellent,Term=5yrs,Income=high)P(y=risky|Credit=excellent,Term=5yrs,Income=high)=67295940729=15180=47294940729=8180 P ( y = s a f e | C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) = 6 729 ∗ 5 9 40 729 = 15 180 P ( y = r i s k y | C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) = 4 729 ∗ 4 9 40 729 = 8 180

由此可知 P(y=safe|Credit=excellent,Term=5yrs,Income=high)> P ( y = s a f e | C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) >
P(y=risky|Credit=excellent,Term=5yrs,Income=high) P ( y = r i s k y | C r e d i t = e x c e l l e n t , T e r m = 5 y r s , I n c o m e = h i g h ) ,所以该样本的类别标签应预测为 safe s a f e

朴素贝叶斯分类器优缺点

优点:逻辑简单,只需要知道贝叶斯公司、全概率公式即可。
缺点:朴素贝叶斯分类器是建立在“属性条件独立性假设”基础上的,而现实中往往各个属性之间是有联系的并不是独立地对分类结果产生影响。

朴素贝叶斯分类器种类

依据样本服从的概率分布的不同, 朴素贝叶斯分类器可分为高斯贝叶斯分类器、多项式贝叶斯分类器、伯努利贝叶斯分类器等。

  • 高斯贝叶斯分类器:假设属性的条件概率分布满足高斯分布。
  • 多项式贝叶斯分类器:假设属性的条件概率分布满足多项式分布。
  • 伯努利贝叶斯分类器:假设属性的条件概率分布满足二项分布。

参考文献

周志华. 机器学习 [D]. 清华大学出版社,2016.
华校专、王正林. Python大战机器学习 [D]. 电子工业出版社,2017.

ellipse
图1

更多资料请移步github:
https://github.com/GarryLau/MachineLearning

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值