0

好的,所以我训练了一个 NaiveBayes 电影评论分类器……但是,当我针对负面评论(从我复制并粘贴到 txt 文件中的网站)运行它时,我得到了“pos”……我做错了什么吗?下面是代码:

import nltk, 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): 
    document_words = set(document) 
    features = {}
    for word in word_features:
        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)) 
classifier.show_most_informative_features(5)
>>>0.67
>>>Most Informative Features
      contains(thematic) = True              pos : neg    =      8.9 : 1.0
        contains(annual) = True              pos : neg    =      8.9 : 1.0
       contains(miscast) = True              neg : pos    =      8.7 : 1.0
      contains(supports) = True              pos : neg    =      6.9 : 1.0
    contains(unbearable) = True              neg : pos    =      6.7 : 1.0

f = open('negative_review.txt','rU')
fraw = f.read()
review_tokens =nltk.word_tokenize(fraw)
docfts = document_features(review_tokens)

classifier.classify(docfts)
>>>    'pos'

更新多次重新运行程序后,它现在准确地将我的负面评论归类为负面......有人可以帮我理解为什么吗?或者这是普通的巫术?

4

1 回答 1

1

分类器不是 100% 准确的。更好的测试是查看分类器如何处理多个电影评论。我看到分类器的准确率是 67%,这意味着 1/3 的评论会被错误分类。您可以尝试使用不同的分类器或不同的特征来改进模型(尝试 n-gram 和 word2vec)。

于 2017-03-01T06:14:53.743 回答