【发布时间】:2017-07-08 11:36:49
【问题描述】:
从这里:http://www.nltk.org/book/ch06.html(第 1.3 节)。
作者引用的准确度为 0.81。
当然,由于 random.shuffle 涉及到一些随机性,但无论我运行多少次,我都无法超过 0.73。
(还有一个额外的奇怪之处在于,作者声称下面的 word_features 包含 2000 个最常用的词,但事实并非如此(与 list(all_words.most_common(2000) 相比)。)
import nltk
import random
from nltk.corpus import movie_reviews
documents = [(list(movie_reviews.words(fileid)), category)\
for category in movie_reviews.categories()\
for fileid in movie_reviews.fileids(category)]
random.shuffle(documents)
all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())
word_features = list(all_words)[:2000]
def document_features(document, words_to_use = word_features):
document_words = set(document)
features = {}
for word in words_to_use:
features['contains({})'.format(word)] = (word in document_words)
return features
featuresets = [(document_features(d), c) for (d,c) in documents]
train_set, test_set = featuresets[100:], featuresets[:100]
classifier = nltk.NaiveBayesClassifier.train(train_set)
print(nltk.classify.accuracy(classifier, test_set))
【问题讨论】:
-
如果你使用 2000 个最常用的词,你的准确率会更高吗?
-
@lenz,不,准确度更差,因为 2000 个最常见的词(“the”、“a”、“he”...)中的大多数都包含很少的类别信息。
标签: python nltk document-classification