【问题标题】:NLTK accuracy: "ValueError: too many values to unpack"NLTK 准确性:“ValueError:解包的值太多”
【发布时间】:2015-08-10 12:56:14
【问题描述】:

我正在尝试使用 NLTK 工具包对 Twitter 上的一部新电影进行一些情感分析。我遵循了 NLTK 'movie_reviews' 示例,并构建了自己的 CategorizedPlaintextCorpusReader 对象。当我打电话给nltk.classify.util.accuracy(classifier, testfeats) 时,问题就出现了。代码如下:

import os
import glob
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews

def word_feats(words):
        return dict([(word, True) for word in words])

negids = movie_reviews.fileids('neg')
posids = movie_reviews.fileids('pos')

negfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'neg') for f in negids]
posfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'pos') for f in posids]

trainfeats = negfeats + posfeats

# Building a custom Corpus Reader
tweets = nltk.corpus.reader.CategorizedPlaintextCorpusReader('./tweets', r'.*\.txt', cat_pattern=r'(.*)\.txt')
tweetsids = tweets.fileids()
testfeats = [(word_feats(tweets.words(fileids=[f]))) for f in tweetsids]

print 'Training the classifier'
classifier = NaiveBayesClassifier.train(trainfeats)

for tweet in tweetsids:
        print tweet + ' : ' + classifier.classify(word_feats(tweets.words(tweetsids)))

classifier.show_most_informative_features()

print 'accuracy:', nltk.classify.util.accuracy(classifier, testfeats)

在到达最后一行之前似乎一切正常。那是我得到错误的时候:

>>> nltk.classify.util.accuracy(classifier, testfeats)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/nltk/classify/util.py", line 87, in accuracy
    results = classifier.classify_many([fs for (fs,l) in gold])
ValueError: too many values to unpack

有人发现代码中有什么问题吗?

谢谢。

【问题讨论】:

    标签: python nltk text-classification


    【解决方案1】:

    错误信息

    File "/usr/lib/python2.7/dist-packages/nltk/classify/util.py", line 87, in accuracy
      results = classifier.classify_many([fs for (fs,l) in gold])
    ValueError: too many values to unpack
    

    出现是因为gold 中的项目无法解压缩为 2 元组 (fs,l)

    [fs for (fs,l) in gold]  # <-- The ValueError is raised here
    

    如果gold 等于[(1,2,3)],您将得到相同的错误,因为不能将三元组(1,2,3) 解压缩为二元组(fs,l)

    In [74]: [fs for (fs,l) in [(1,2)]]
    Out[74]: [1]
    In [73]: [fs for (fs,l) in [(1,2,3)]]
    ValueError: too many values to unpack
    

    gold 可能隐藏在 nltk.classify.util.accuracy 的实现中,但这暗示您的输入 classifiertestfeats 的“形状”错误。

    分类器没有问题,因为调用accuracy(classifier, trainfeats) 作品:

    In [61]: print 'accuracy:', nltk.classify.util.accuracy(classifier, trainfeats)
    accuracy: 0.9675
    

    问题一定出在testfeats


    trainfeatstestfeats 进行比较。 trainfeats[0] 是一个包含字典和分类的 2 元组:

    In [63]: trainfeats[0]
    Out[63]: 
    ({u'!': True,
      u'"': True,
      u'&': True,
      ...
      u'years': True,
      u'you': True,
      u'your': True},
     'neg')           # <---  Notice the classification, 'neg'
    

    testfeats[0] 只是一个字典,word_feats(tweets.words(fileids=[f]))

    testfeats = [(word_feats(tweets.words(fileids=[f]))) for f in tweetsids]
    

    因此,要解决此问题,您需要将testfeats 定义为更像trainfeats——word_feats 返回的每个字典都必须与分类配对。

    【讨论】:

    • 你是对的。正如您所说,问题在于 testfeats 不是 [dictionary, classification_string] 的元组。谢谢
    猜你喜欢
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多