我正在寻找一个简单的例子,如何运行一个多项朴素贝叶斯分类器。我从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我的问题是..。
发布于 2013-07-05 21:34:38
原始代码对前100个正负示例进行了训练,然后对其余部分进行分类。您已经删除了边界,并在培训和分类阶段使用了每个示例,换句话说,您具有重复的特性。要解决这个问题,将数据集分成两组,训练和测试。
混淆矩阵较高(或不同),因为您正在就不同的数据进行培训。
混淆矩阵是对准确性的度量,并显示假阳性的数量等。在这里阅读更多:matrix
发布于 2013-11-11 19:56:54
我使用了最初的代码,训练集中只有前100条条目,并且仍然有这样的警告。我的产出是:
In [6]: %run testclassifier.py
C:\Users\..\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\feature_selection\univariate_selecti
on.py:319: UserWarning: Duplicate scores. Result may depend on feature ordering.There are probably duplicate features, o
r you used a classification score for a regression task.
warn("Duplicate scores. Result may depend on feature ordering."
Confusion matrix:
427 473
132 768https://stackoverflow.com/questions/17468107
复制相似问题