Naive base classifier of nltk giving unhashable type error

Deadly 提交于 2019-12-12 04:37:09

问题


Following is the code that I wrote using nltk and 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)

I am getting this error

traceback (most recent call last): File "naive_bayes_application.py", line 37, in classifier = nltk.NaiveBayesClassifier.train(training_set) File "C:\Users\jshub\Anaconda3\lib\site-packages\nltk\classify\naivebayes.py", line 198, in train feature_freqdist[label, fname][fval] += 1 TypeError: unhashable type: 'set'

Please help.


回答1:


Just in def find_features while constructing the features dictionary pass value in normal brackets.

example:

for w in word_features:
    features[w] = (w in words)


来源:https://stackoverflow.com/questions/45814086/naive-base-classifier-of-nltk-giving-unhashable-type-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!