机器学习实战 - 读书笔记(04) - 朴素贝叶斯

核心公式 - 贝叶斯准则

\[p(c|x) = \frac{p(x|c)p(c)}{p(x)} \]

  • p(c|x) 是在x发生的情况下,c发生的概率。
  • p(x|c) 是在c发生的情况下,x发生的概率。
  • p(c) 是c发生的概率。
  • p(x) 是x发生的概率。

规则

如果P(c₁|x) > P(c₂|x),那么属于类别c₁。
如果P(c₁|x) < P(c₂|x),那么属于类别c₂。

等价变化

\[p(c1|x) = \frac{p(x|c1)p(c1)}{p(x)} \]

\[p(c2|x) = \frac{p(x|c2)p(c2)}{p(x)} \]

Therefore, comparing p(c1|x) and p(c2|x)
are same as comparing
\(\frac{p(x|c1)p(c1)}{p(x)}\) and \(\frac{p(x|c2)p(c2)}{p(x)}\)
same as comparing
\(p(x|c1)p(c1)\) and \(p(x|c2)p(c2)\)

多个独立特征的变化

p(x|c1)中,x是多个独立特征,即\(x=x_0,x_1...x_n\),
则: \(p(x|c1)=p(x_0,x_1...x_n|c1)\)
\(p(x|c1)=p(x_0|c1)p(x_1|c1)...p(x_n|c1)\)

下溢出问题

为了解决下溢出问题,这是由于太多很小的数相乘造成的,所以程序会下溢出或者得到不正确的答案。
在代数中有ln(a*b) = ln(a)+ln(b),于是通过求对数可以避免下溢出或者浮点数舍入导致的错误。同时,采用自然对数进行处理不会有任何损失。
Therefore, comparing p(c1|x) and p(c2|x)
same as comparing
\(log(p(x_0|c1)) + log(p(x_1|c1)) + ... + log(p(x_n|c1) + log(p(c1))\) and
\(log(p(x_0|c2)) + log(p(x_1|c2)) + ... + log(p(x_n|c2) + log(p(c2))\)

实际应用

  • 过滤侮辱性留言
  • 过滤垃圾邮件
posted @ 2016-06-17 00:04  SNYang  阅读(859)  评论(0编辑  收藏  举报