nltk的朴素基础分类器给出无法散列的类型错误 [英] Naive base classifier of nltk giving unhashable type error

查看:175
本文介绍了nltk的朴素基础分类器给出无法散列的类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我收到此错误

回溯(最近一次通话最近): 在第37行的文件"naive_bayes_application.py"中 分类器= nltk.NaiveBayesClassifier.train(training_set) 文件"C:\ Users \ jshub \ Anaconda3 \ lib \ site-packages \ nltk \ classify \ naivebayes.py", 198号线,在火车上 feature_freqdist [label,fname] [fval] + = 1 TypeError:不可散列的类型:'set'

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'

请帮助.

推荐答案

仅在def find_features中使用,而在普通括号中构造功能字典传递值.

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

示例:

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

这篇关于nltk的朴素基础分类器给出无法散列的类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆