nltk的朴素基本分类器给出了不可靠的类型错误

时间:2017-08-22 09:40:27

标签: python nltk naivebayes

以下是我使用nltk和Python编写的代码。

import nltk
import random
from nltk.corpus import movie_reviews
#from sklearn.naive_bayes import GaussianNB
documents = [(list(movie_reviews.words(fileid)), category)
    for category in movie_reviews.categories()
    for fileid in movie_reviews.fileids(category)]

random.shuffle(documents)

#print(documents[1:3]) 

all_words= []
for w in movie_reviews.words():
    all_words.append(w.lower())

all_words = nltk.FreqDist(all_words)
#print(all_words.most_common(15))
#print(all_words["great"])
word_features = list(all_words.keys())[:3000]

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

    return features

#print((find_features(movie_reviews.words('neg/cv000_29416.txt'))))

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

training_set  = featuresets[:1900]
testing_set = featuresets[1900:]

classifier = nltk.NaiveBayesClassifier.train(training_set)
print("Naive Bayes Algo Accuracy percent:", (nltk.classify.accuracy(classifier, testing_set))*100)
classifier.show_most_informative_features(15)

# clf = GaussianNB()
# clf.fit(training_set)

我收到此错误

  

追踪(最近一次调用最后一次):         文件" naive_bayes_application.py",第37行,in           classifier = nltk.NaiveBayesClassifier.train(training_set)         文件" C:\ Users \ jshub \ Anaconda3 \ lib \ site-packages \ nltk \ classify \ naivebayes.py",   198号线,在火车上           feature_freqdist [label,fname] [fval] + = 1       TypeError:不可用类型:'设置'

请帮忙。

1 个答案:

答案 0 :(得分:1)

在构造特征字典时,在def find_features中传递普通括号中的值。

示例:

         HR
Banks 1  1
Banks 2  3
Banks 3  1