gpt4 book ai didi

python-3.x - 来自 MultinomialNB : float() argument must be a string or a number 的类型错误

转载 作者:行者123 更新时间:2023-11-30 09:07:04 27 4
gpt4 key购买 nike

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

TypeError: float() argument must be a string or a number, not '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

第一行创建一个由 (word, {True})(word, {False}) 对组成的 featuresets 列表,即第二个元素是一个集合SklearnClassifier 不希望将其作为标签。

<小时/>

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

关于python-3.x - 来自 MultinomialNB : float() argument must be a string or a number 的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49415195/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com