【问题标题】:Naive bayes algorithm using python3.5.2使用python3.5.2的朴素贝叶斯算法
【发布时间】:2016-12-01 04:11:01
【问题描述】:

我正在尝试使用 python 3.5.2 在数据集中实现朴素贝叶斯算法,但它给了我一个错误:

ValueError:解包的值太多(预计 2 个)

谁能帮助我解决这个问题?我对python很陌生。

import csv
import nltk
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
f = open("C:\Python_code\pima-indians-diabetes.csv")
csv_f = csv.reader(f)
import numpy
csv_f = numpy.random.rand(100, 5)
numpy.random.shuffle(csv_f)
training, test = csv_f[:80,:], csv_f[80:,:]
print(training,test)
classifier = nltk.NaiveBayesClassifier.train(training)
print("Naive Bayes Algo Accuracy:", (nltk.classify.accuracy(classifier,test))*100)

【问题讨论】:

  • 哪一行会引发错误?
  • classifier = nltk.NaiveBayesClassifier.train(training) print("Naive Bayes Algo Accuracy:", (nltk.classify.accuracy(classifier,test))*100) 这行引发了错误跨度>
  • 您似乎向分类器提供了不正确的数据。尝试检查nltk.NaiveBayesClassifier.train 的输入并将其与您的变量training 进行比较。
  • 我尝试用 sklearn.naive_bayes 中的以下代码替换最后 2 行 import GaussianNB clf = GaussianNB() clf.fit(features_training,labels_training) pred = clf.predict(features_test) from sklearn。 metrics import accuracy_score print (accuracy_score(pred,labels_test)) 但它给了我 NameError: name 'features_training' is not defined
  • @Fomalhaut 你能解释一下你说的话吗?

标签: python python-3.x


【解决方案1】:

阅读文档如何正确使用nltk.NaiveBayesClassifierhttp://www.nltk.org/_modules/nltk/classify/naivebayes.html

您应该为train 准备正确的输入。这是一个元组列表(featureset, label)

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 2012-02-21
    • 2011-12-28
    • 2021-04-09
    • 2015-03-06
    • 2016-08-18
    • 2010-10-19
    • 2012-09-23
    • 2019-02-15
    相关资源
    最近更新 更多