除了其他来源之外,我还使用 Stackoverflow 上的各种帖子,尝试实现自己的 PHP 分类器,将推文分为正面、中性和负面类别。在编码之前,我需要理清流程。我的思路和例子如下:
p(class) * p(words|class)
Bayes theorem: p(class|words) = ------------------------- with
p(words)
assumption that p(words) is the same for every class leads to calculating
arg max p(class) * p(words|class) with
p(words|class) = p(word1|class) * p(word2|topic) * ... and
p(class) = #words in class / #words in total and
p(word, class) 1
p(word|class) = -------------- = p(word, class) * -------- =
p(class) p(class)
#times word occurs in class #words in total #times word occurs in class
--------------------------- * --------------- = ---------------------------
#words in total #words in class #words in class
Example:
------+----------------+-----------------+
class | words | #words in class |
------+----------------+-----------------+
pos | happy win nice | 3 |
neu | neutral middle | 2 |
neg | sad loose bad | 3 |
------+----------------+-----------------+
p(pos) = 3/8
p(neu) = 2/8
p(meg) = 3/8
Calculate: argmax(sad loose)
p(sad loose|pos) = p(sad|pos) * p(loose|pos) = (0+1)/3 * (0+1)/3 = 1/9
p(sad loose|neu) = p(sad|neu) * p(loose|neu) = (0+1)/3 * (0+1)/3 = 1/9
p(sad loose|neg) = p(sad|neg) * p(loose|neg) = 1/3 * 1/3 = 1/9
p(pos) * p(sad loose|pos) = 3/8 * 1/9 = 0.0416666667
p(neu) * p(sad loose|neu) = 2/8 * 1/9 = 0.0277777778
p(neg) * p(sad loose|neg) = 3/8 * 1/9 = 0.0416666667 <-- should be 100% neg!
正如你所看到的,我用积极的(“快乐获胜很好”)、中性的(“中性中间”)和消极的(“悲伤的松散的坏”)推文“训练”了分类器。为了防止由于所有类中缺少一个单词而导致概率为零的问题,我使用拉普拉斯(或“添加一”)平滑,请参阅“(0+1)”。
我基本上有两个问题:
- 这是正确的实施蓝图吗?还有改进的空间吗?
- 对推文(“sad loes”)进行分类时,预计它 100% 属于“neg”类,因为它只包含否定词。然而,拉普拉斯平滑使事情变得更加复杂:类 pos 和 neg 具有相同的概率。有解决方法吗?
推理中有两个主要元素需要改进。
首先,你应该改进你的平滑方法:
- 应用拉普拉斯平滑时,应将其应用于所有测量,而不仅仅是分母为零的测量。
- 此外,这种情况下的拉普拉斯平滑通常由 (c+1)/(N+V) 给出,其中 V 是词汇量(例如,参见维基百科 http://en.wikipedia.org/wiki/Additive_smoothing).
因此,使用您定义的概率函数(这可能不是最合适的,见下文):
p(sad loose|pos) = (0+1)/(3+8) * (0+1)/(3+8) = 1/121
p(sad loose|neu) = (0+1)/(3+8) * (0+1)/(3+8) = 1/121
p(sad loose|neg) = (1+1)/(3+8) * (1+1)/(3+8) = 4/121 <-- would become argmax
此外,首先计算概率的更常见方法是:
(number of tweets in class containing term c) / (total number of tweets in class)
例如,在上面给出的有限训练集中,不考虑平滑,p(sad|pos) = 0/1 = 0,并且 p(sad|neg) = 1/1 = 1。当训练集大小增加时,数字将变得更有意义。例如如果您有 10 条属于负面类别的推文,其中 4 条出现“悲伤”,那么 p(sad|neg) 将为 4/10。
关于朴素贝叶斯算法输出的实际数字:您不应该期望算法为每个类别分配实际概率;相反,类别顺序更重要。具体来说,使用 argmax 将为您提供算法对该类的最佳猜测,但不是它的概率。为 NB 结果分配概率是另一回事;例如,参见article http://www.bradblock.com/Transforming_Classifier_Scores_into_Accurate_Multiclass_Probability_Estimates.pdf讨论这个问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)