gpt4 book ai didi

python-2.7 - NLTK NaiveBayesClassifier 抛出属性错误,指出 'list' 对象没有属性 'items'

转载 作者:行者123 更新时间:2023-11-30 09:10:17 25 4
gpt4 key购买 nike

我正在尝试使用 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'

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

最佳答案

将您的特征值作为字典。

来源: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')

关于python-2.7 - NLTK NaiveBayesClassifier 抛出属性错误,指出 'list' 对象没有属性 'items',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41014907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com