python - Naive base classifier of nltk giving unhashable type error -
following code wrote using nltk , python.
import nltk import random nltk.corpus import movie_reviews #from sklearn.naive_bayes import gaussiannb documents = [(list(movie_reviews.words(fileid)), category) category in movie_reviews.categories() fileid in movie_reviews.fileids(category)] random.shuffle(documents) #print(documents[1:3]) all_words= [] 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 = {} 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) (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 getting 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.
wiki
Comments
Post a Comment