python - 使用 Python 示例对多项式朴素贝叶斯分类器进行分类

标签 python machine-learning classification nltk bayesian

我正在寻找一个关于如何运行多项式朴素贝叶斯分类器的简单示例。我从 StackOverflow 看到这个例子:

Implementing Bag-of-Words Naive-Bayes classifier in NLTK

import numpy as np
from nltk.probability import FreqDist
from nltk.classify import SklearnClassifier
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline

pipeline = Pipeline([('tfidf', TfidfTransformer()),
                     ('chi2', SelectKBest(chi2, k=1000)),
                     ('nb', MultinomialNB())])
classif = SklearnClassifier(pipeline)

from nltk.corpus import movie_reviews
pos = [FreqDist(movie_reviews.words(i)) for i in movie_reviews.fileids('pos')]
neg = [FreqDist(movie_reviews.words(i)) for i in movie_reviews.fileids('neg')]
add_label = lambda lst, lab: [(x, lab) for x in lst]
#Original code from thread:
#classif.train(add_label(pos[:100], 'pos') + add_label(neg[:100], 'neg'))
classif.train(add_label(pos, 'pos') + add_label(neg, 'neg'))#Made changes here

#Original code from thread:    
#l_pos = np.array(classif.batch_classify(pos[100:]))
#l_neg = np.array(classif.batch_classify(neg[100:]))
l_pos = np.array(classif.batch_classify(pos))#Made changes here
l_neg = np.array(classif.batch_classify(neg))#Made changes here
print "Confusion matrix:\n%d\t%d\n%d\t%d" % (
          (l_pos == 'pos').sum(), (l_pos == 'neg').sum(),
          (l_neg == 'pos').sum(), (l_neg == 'neg').sum())

运行此示例后我收到了警告。

C:\Python27\lib\site-packages\scikit_learn-0.13.1-py2.7-win32.egg\sklearn\feature_selection\univariate_selection.py:327: 
UserWarning: Duplicate scores. Result may depend on feature ordering.There are probably duplicate features, 
or you used a classification score for a regression task.
warn("Duplicate scores. Result may depend on feature ordering."

Confusion matrix:
876 124
63  937

所以,我的问题是..

  1. 谁能告诉我这条错误信息是什么意思?
  2. 我对原始代码做了一些修改,但为什么混淆矩阵结果比原始线程中的结果高很多?
  3. 我如何测试这个分类器的准确性?

最佳答案

原始代码对前 100 个正面和负面示例进行训练,然后对其余部分进行分类。您已经删除了边界并在训练和分类阶段都使用了每个示例,换句话说,您具有重复的功能。要解决此问题,请将数据集分成两组,训练和测试。

混淆矩阵更高(或不同),因为您正在使用不同的数据进行训练。

混淆矩阵是准确性的衡量标准,显示误报的数量等。在此处阅读更多信息:http://en.wikipedia.org/wiki/Confusion_matrix

关于python - 使用 Python 示例对多项式朴素贝叶斯分类器进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17468107/

相关文章:

python - 执行 scikit-learn 线性回归模型时出现问题

python - 逆变换预测结果

nlp - 文档分析和标记

python - 如何在 .fit() 方法中对多个标签(trainy)使用一种热编码?

python - IDE 无法识别子类中的函数签名与基方法匹配

python - 在 Python 脚本中比较 SNMP OID

opencv - 如何找到图像中的极角点?

algorithm - 值的恒定时间分级

python - 如何在opencv-python中将输出帧全部组合在一起?

python - 调用所有可能的函数组合