NLTK NaiveBayesClassifier抛出属性错误,说明'list'对象没有属性'items'

时间:2016-12-07 10:23:36

标签: python-2.7 machine-learning nlp nltk ipython-notebook

我正在尝试使用NLTK的朴素贝叶斯分类器训练数据集,但我的终端不断抛出此错误

# Applying Naive Bayes
training_set = featursets[:2000]
testing_set = featursets[2000:]
classifier = nltk.NaiveBayesClassifier.train(training_set)
print "Naive bayes classifier accuracy % = ", (nltk.classify.accuracy(classifier, testing_set)*100)
classifier.show_informative_features(30)

错误说:

AttributeError                            
Traceback (most recent call last)
<ipython-input-69-2a409562c9f8> in <module>()
  2 training_set = featursets[:2000]
  3 testing_set = featursets[2000:]
  ----> 4 classifier = nltk.NaiveBayesClassifier.train(featursets)
  5 print "Naive bayes classifier accuracy % = "(nltk.classify.accuracy(classifier, testing_set)*100)
  6 classifier.show_informative_features(30)

  /home/satyaki/.local/lib/python2.7/site-packages/nltk/classify/naivebayes.pyc in train(cls, labeled_featuresets, estimator)
194         for featureset, label in labeled_featuresets:
195             label_freqdist[label] += 1
--> 196             for fname, fval in featureset.items():
197                 # Increment freq(fval|label, fname)
198                 feature_freqdist[label, fname][fval] += 1

AttributeError: 'list' object has no attribute 'items'

但我不确定这里出了什么问题。任何帮助,伙计们?

1 个答案:

答案 0 :(得分:1)

将您的要素值设为字典。

来源:reference link

火车数据片段[0]:

({'able': True,
  'absurdly': True,
  'alone': True,
  'american': True,
  'americans': True,
  'anyone': True,
  'appearance': True,
  'applauding': True,
  'atrocious': True,
  'audience': True,
  'audiences': True,
  'aykroyd': True,
  'bacall': True,
  'band': True,
  'banter': True,
  'bicker': True,
  'bits': True,
  'brothers': True,
  'chief': True,
  'clude': True,
  'comedy': True,
  'commander': True,
  'counted': True,
  'crap': True,
  'current': True,
  'dan': True,
  'dialogue': True,
  'discriminating': True,
  'dorothy': True,
  'drowned': True,
  'elvis': True,
  'especially': True,
  'even': True,
  'ex': True,
  'exchange': True,
  'fellow': True,
  'film': True,
  'fine': True,
  'first': True,
  'fit': True,
  'forget': True,
  'former': True,
  'funny': True,
  'garner': True,
  'gay': True,
  'get': True,
  'gets': True,
  'going': True,
  'grumpy': True,
  'heard': True,
  'heaven': True,
  'help': True,
  'holiday': True,
  'honestly': True,
  'immediately': True,
  'impersonator': True,
  'including': True,
  'ing': True,
  'ish': True,
  'jack': True,
  'james': True,
  'john': True,
  'joke': True,
  'judas': True,
  'lady': True,
  'lauren': True,
  'lemmon': True,
  'macarena': True,
  'march': True,
  'marching': True,
  'men': True,
  'merely': True,
  'mine': True,
  'moment': True,
  'movie': True,
  'musical': True,
  'non': True,
  'number': True,
  'offensively': True,
  'old': True,
  'older': True,
  'one': True,
  'overbearing': True,
  'penis': True,
  'perfect': True,
  'performing': True,
  'raw': True,
  'references': True,
  'resist': True,
  'rest': True,
  'ritual': True,
  'road': True,
  'room': True,
  'scores': True,
  'seeing': True,
  'silence': True,
  'single': True,
  'slot': True,
  'sold': True,
  'star': True,
  'submit': True,
  'supporting': True,
  'sure': True,
  'talkin': True,
  'tarheels': True,
  'yup': True},
 'negative')