我认为我已经正确地实现了大部分内容。有一部分让我感到困惑:
零频率问题: 当属性值未与每个类值一起出现时,为每个属性值-类组合(拉普拉斯估计器)的计数添加1。
这是我的一些客户端代码:
//Clasify
string text = "Claim your free Macbook now!";
double posteriorProbSpam = classifier.Classify(text, "spam");
Console.WriteLine("-------------------------");
double posteriorProbHam = classifier.Classify(text, "ham");
现在假设单词“free”在训练数据中出现过。
//Training
classifier.Train("ham", "Attention: Collect your Macbook from store.");
*Lot more here*
classifier.Train("spam", "Free macbook offer expiring.");
但是这个单词只在我的类别为“垃圾邮件”的训练数据中出现,而不是在“正常邮件”中。所以当我计算后验概率 ham 时,当遇到单词 “free” 时该怎么办。
P(free|spam) = (10+1)/(50+1)
,P(spam) = (50+1)/(150+1)
,P(free) = (15+1)/(150+1)
。您还可以使用小于1的数字(例如0.1,通常称为“alpha”),因为它对应于在这些概率上使用Dirichlet-alpha分布作为您的prior。 - DanicaP(垃圾邮件 | 单词1,单词2,...) > 1
?我可能错了,但我认为不应该发生这种情况...不过,例如\sum_w P(w | 垃圾邮件)
将会大于1。 - Danica