我正在尝试比较多项式,二项式和伯努利分类器的性能,但出现错误:


  TypeError:float()参数必须是字符串或数字,而不是“ set”


下面的代码是MultinomialNB

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])

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]

def look_for_features(document):
    words = set(document)
    features = {}
    for x in word_features:
        features[x] = {x in words}
    return features

#feature set will be finding features and category
featuresets = [(look_for_features(rev), category) for (rev, category) in documents]

training_set = featuresets[:1400]
testing_set = featuresets[1400:]

#Multinomial
MNB_classifier = SklearnClassifier(MultinomialNB())
MNB_classifier.train(training_set)
print ("Accuracy: ", (nltk.classify.accuracy(MNB_classifier,testing_set))*100)


错误似乎在MNB_classifier.train(training_set)中。
此代码中的错误类似于错误here

最佳答案

更改...

features[x] = {x in words}


至...

features[x] = x in words


第一行创建对featuresets(word, {True})的列表(word, {False}),即第二个元素是setSklearnClassifier不应将此作为标签。



该代码非常类似于"Creating a module for Sentiment Analysis with NLTK"中的代码。作者在那里使用了元组(x in words),但这与x in words没什么不同。

关于python-3.x - 来自MultinomialNB的TypeError:float()参数必须为字符串或数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49415195/

10-12 19:34