【问题标题】:AttributeError: 'NoneType' object has no attribute 'items' for classifier = nltk.NaiveBayesClassifier.train(training_set)AttributeError: 'NoneType' 对象没有分类器的属性'items' = nltk.NaiveBayesClassifier.train(training_set)
【发布时间】:2017-12-16 11:25:13
【问题描述】:

我收到 AttributeError 的此错误:'NoneType' object has no attribute 'items.代码如下:

import nltk
import random
from nltk.corpus import movie_reviews
from nltk.classify import NaiveBayesClassifier
from nltk.probability import FreqDist

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 = []
for w in movie_reviews.words():
all_words.append(w.lower())

all_words =nltk.FreqDist(all_words)

word_features = list(all_words.keys())[:3000]
print (word_features)

def find_features(documents):
    words = set(documents)
    features = {}
    for w in word_features:
        features[w] = (w in words)


featuresets = [(find_features(rev), category) for (rev, category) in 
documents]

training_set = featuresets[:1500]
testing_set = featuresets[1500:]


classifier = nltk.NaiveBayesClassifier.train(training_set)

print ("Naive Bayes Classifier Algo Accuracy: ",nltk.classify.accuracy(classifier, testing_set))*100)

classifier.show_most_informative_features(15)

我一直在关注视频教程,那里的代码相同。在那里它运行良好,但在这里它显示以下错误:

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

换行

分类器 = nltk.NaiveBayesClassifier.train(training_set)

什么原因和解决方法?

【问题讨论】:

    标签: machine-learning nlp nltk text-classification naivebayes


    【解决方案1】:

    您没有从函数返回列表。在你的 find_feature 函数中使用:

    def find_features(documents):
        words = set(documents)
        features = {}
        for w in word_features:
            features[w] = (w in words)
        return features #ADD THIS LINE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-14
      • 2013-09-22
      • 1970-01-01
      • 2015-09-30
      • 2020-09-22
      • 2019-01-01
      • 2021-12-26
      • 2019-07-23
      相关资源
      最近更新 更多