簡體   English   中英

用Python實例分類多項式朴素貝葉斯分類器

[英]Classifying Multinomial Naive Bayes Classifier with Python Example

我正在尋找一個關於如何運行Multinomial Naive Bayes分類器的簡單示例。 我從StackOverflow中看到了這個例子:

在NLTK中實現Bag-of-Words朴素貝葉斯分類器

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

我使用原始代碼只有訓練集的前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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM