2013-07-04 35 views
5

Multinomial Naive Bayes Classifierを実行する方法の簡単な例を探しています。私はStackOverflowのからこの例に出くわした:私はこの例を実行した後に警告を受けPythonで多項式Naive Bayesクラシファイアを分類する例

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. この分類子の精度をどのようにテストできますか?

答えて

2

元のコードは、最初と最後の100の例を列挙し、残りを分類します。境界線を削除し、トレーニングと分類フェーズの両方で各例を使用しました。つまり、重複した機能があります。これを修正するには、データセットを2つのセットに分割し、トレーニングとテストを行います。

異なるデータで訓練しているため、混乱マトリクスがより高い(または異なる)ことがあります。

混同マトリックスは、精度の尺度であり、など、ここでもっと読む偽陽性の数を示しています

+0

[答えを受け入れる]にしてください(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235)それは参考になりました場合には – Spaceghost

1

http://en.wikipedia.org/wiki/Confusion_matrix私はトレーニングセットのための唯一の最初の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  768 
関連する問題