我可以使用NaiveBayesClassifier对两个以上的分类进行分类吗?

2024-04-18 15:11:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在NaiveBayesClassifier中看到的大多数示例只有两个:“pos”、“neg”。我想谈谈课文的主题,比如娱乐、体育、电影、政治、文学。这是有可能的训练朴素贝叶斯分类器,或我应该找其他地方?在


Tags: pos示例主题电影分类器地方政治课文
1条回答
网友
1楼 · 发布于 2024-04-18 15:11:15

当然可以。当您将训练集传递到NaiveBayesClassifier.train方法时,它将为训练集中的每个标签创建一个Bayes模型。如果你的训练集有多个标签,那么你的分类器将分为多个标签。如果训练集只有2个标签,那么分类器将只给出两个分类。当您要求分类器分类时,它将返回给定特征集的概率最高的模型。在

在Bayes分类器中,为每个标签建立一个概率模型。选择最符合特征的模型。下面是一个虚构的例子:

import nltk

articles = [({'entertaining':0.6, 'informative':0.2, 'statistical':0.6}, 'sports'),
            ({'entertaining':0.7, 'informative':0.2, 'statistical':0.8}, 'sports'),
            ({'entertaining':0.1, 'informative':0.7, 'statistical':0.2}, 'news'),
            ({'entertaining':0.2, 'informative':0.8, 'statistical':0.3}, 'news'),
            ({'entertaining':0.8, 'informative':0.2, 'statistical':0.1}, 'movies')]

classifier = nltk.NaiveBayesClassifier.train(articles)

label = classifier.classify({'entertaining':0.9, 'informative':0.2, 'statistical':0.1})

print label
#movies    

probabilities = classifier.prob_classify({'entertaining':0.9, 'informative':0.2, 'statistical':0.1})

for sample in probabilities.samples():
    print "{0}: {1}".format(sample, probabilities.prob(sample))
#news:   0.0580
#sports: 0.2999
#movies: 0.6522

相关问题 更多 >