本文整理汇总了Python中sklearn.naive_bayes.MultinomialNB.class_prior方法的典型用法代码示例。如果您正苦于以下问题:Python MultinomialNB.class_prior方法的具体用法?Python MultinomialNB.class_prior怎么用?Python MultinomialNB.class_prior使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.naive_bayes.MultinomialNB
的用法示例。
在下文中一共展示了MultinomialNB.class_prior方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: labelize
# 需要导入模块: from sklearn.naive_bayes import MultinomialNB [as 别名]
# 或者: from sklearn.naive_bayes.MultinomialNB import class_prior [as 别名]
test_set = [(features(words), labelize(category in categories)) for (words, categories) in test_corpus]
# train classifier
# print "Training classifier for '%s'" % category
# classifier = MaxentClassifier.train(train_set, max_iter= 3)
# classifier = NaiveBayesClassifier.train(train_set)
model = MultinomialNB()
classifier = SklearnClassifier(model)
# set priors
classifier._encoder.fit([category, "no"])
# [category, "no"] unless this is true then ["no", category]
flip = classifier.labels()[0] == "no"
categorized_proportion = len([words for (words, categories) in corpus if category in categories]) * 1.0 / len(corpus)
if flip:
model.class_prior = [1-categorized_proportion, categorized_proportion]
else:
model.class_prior = [categorized_proportion, 1-categorized_proportion]
classifier.train(train_set)
# test classifier
test_results = classifier.classify_many([feat for (feat, label) in test_set])
pos_test_set = set(i for i, result in enumerate(test_results) if result == category)
reference_values = [label for (feat, label) in test_set]
pos_ref_set = set(i for i, (feat, label) in enumerate(test_set) if label == category)
accuracy = scores.accuracy(reference_values, test_results)
accuracies.append(accuracy)
precision = scores.precision(pos_ref_set, pos_test_set)
recall = scores.recall(pos_ref_set, pos_test_set)
f1 = scores.f_measure(pos_ref_set, pos_test_set)