【问题标题】:Can't reproduce results in NLTK Book, Document Classification (Chapter 6, section 1.3)无法在 NLTK Book, Document Classification 中重现结果(第 6 章,第 1.3 节)
【发布时间】: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


【解决方案1】:
  1. list(all_words)[:2000] 与实际的[word for word, _ in all_words.most_common(2000)] 相比,绝对不是前 2000 个最常用的词

  2. 不,准确度更差,因为 2000 个最常见的词(“the”、“a”、“he”...)中的大多数都包含很少的类别信息

当然,这些 停用词 并不是那么有用(它们的信息量较少),但考虑到其中有几个(~30 个),其余的(~1900 个)是仍然很好的相关功能。

在这种情况下我会建议做什么:

  1. 不要使用random.shuffle(document)来保证它们的顺序
  2. 使用实际的 2000 个最常见字词作为您的特征(如上面提供的示例)
  3. 发布您的结果,包括 Python 版本和 NLTK 版本。您可以同时检查

python --version

python -c "import nltk; print(nltk.__version__)"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多