Python 训练和测试错误

标签 python python-3.x machine-learning anaconda training-data

当我尝试运行以下代码的最后一部分时,我收到一个错误,我无法弄清楚原因。

import random
combined_list = h_sub_text + s_sub_text
print(len(combined_list))
random.shuffle(combined_list)

training_part = int(len(combined_list) * .7)
print(len(combined_list))
training_set = combined_list[:training_part]
test_set =  combined_list[training_part:]
print (len(train_set))
print (len(test_set))

import nltk.classify.util
from nltk.classify import NaiveBayesClassifier

classifier = NaiveBayesClassifier.train(train_set)

accuracy = nltk.classify.util.accuracy(classifier, test_set)

print("Accuracy is: ", accuracy * 100)

我收到这个错误:

ValueError             Traceback (most recent call last)
<ipython-input-57-151936e75238> in <module>()
  2 from nltk.classify import NaiveBayesClassifier

----> 4 classifier = NaiveBayesClassifier.train(training_set)

  C:\Program Files (x86)\Anaconda3\lib\site-packages\nltk\classify\naivebayes.py in train(cls, labeled_featuresets, estimator)

--> 194         for featureset, label in labeled_featuresets:
195             label_freqdist[label] += 1
196             for fname, fval in featureset.items():

ValueError: too many values to unpack (expected 2)

提前致谢。

最佳答案

问题的根源在于 train_set 传递给 NaiveBayesClassifier.train() 的值。要真正知道我们会知道它的外观。 无论是什么导致“nltk”模块出现错误。

来自 NLTK 源代码 http://www.nltk.org/_modules/nltk/classify/naivebayes.html :

@classmethod
def train(cls, labeled_featuresets, estimator=ELEProbDist):
   """
   :param labeled_featuresets: A list of classified featuresets,
       i.e., a list of tuples ``(featureset, label)``.

train() 的参数是一个元组列表。因此,考虑到您在尝试解压缩太多值时遇到的错误,而预期只有 2 个值,这不是您传递的内容。普通数组或大于 2 的数组数组。

关于Python 训练和测试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43358042/

相关文章:

python - 如何在嵌套的 GridSpec 中拥有辅助 y 轴?

python - 字符串模板 4 和 Python

python - 类型错误 : 'Food' object does not support indexing

python - 从 SQL 表(Python)中选择数据时如何去掉括号?

machine-learning - 训练 TensorFlow 修改图像

python - 将 datetime.min 转换为偏移感知日期时间

python - 调用异常 : GraphViz's executables not found (Python)

Python - 有没有一种方法可以对包含日期和时间的字符串列表进行排序

R:如何在数据框中输出唯一的字符串(文本)?

Python Sklearn 线性回归产生不正确的系数值