我使用Python NLTK中的naivebayes分类器计算以下示例的概率分布:import nltk
def main():
train = [(dict(feature=1), 'class_x'), (dict(feature=0), 'class_x'), (dict(feature=0), 'class_y'), (dict(feature=0), 'class_y')]
test = [dict(feature=1)]
classifier = nltk.classify.NaiveBayesClassifier.train(train)
print("classes available: ", sorted(classifier.labels()))
print ("input assigned to: ", classifier.classify_many(test))
for pdist in classifier.prob_classify_many(test):
print ("probability distribution: ")
print ('%.4f %.4f' % (pdist.prob('class_x'), pdist.prob('class_y')))
if __name__ == '__main__':
main()
在训练数据集中有两个类(class_x和class_y)。每个类都有两个输入。对于类_x,第一个输入特性的值为1,第二个输入特性的值为0。对于类\ y,两个输入特征的值都为0。测试数据集由一个输入组成,值为1。在
当我运行代码时,输出是:
^{pr2}$
为了得到每个类的概率或可能性,分类器应该将类的先验值(在本例中为0.5)乘以类中每个特征的概率。应考虑平滑处理。在
我通常使用与此类似的公式(或类似的变体):
p(特征|类)=类的先验值*类中特征的频率+1/类中的总特征+词汇大小。平滑可能会有所不同,并且会稍微改变结果。在
在上面的示例代码中,分类器是如何精确计算概率分布的?使用的公式是什么?在
我检查了here和here,但是没有得到关于计算是如何完成的任何信息。在
提前谢谢。在