我试图将句子分类为各种情感(其中有八个),但是在运行代码时出现此错误。抱歉,我真的是Python的新手,所以我不确定在这里发生了什么。
ValueError
Traceback (most recent call last)
<ipython-input-66-40fb2c79238c> in <module>
18 test_data = data[4800:]
19
---> 20 classfier= nltk.NaiveBayesClassifier.train(train_data)
21 print(nltk.classify.accuracy(classifier, test_data))
~/anaconda3/lib/python3.7/site-packages/nltk/classify/naivebayes.py in train(cls, labeled_featuresets, estimator)
204 # Count up how many times each feature value occurred, given
205 # the label and featurename.
--> 206 for featureset, label in labeled_featuresets:
207 label_freqdist[label] += 1
208 for fname, fval in featureset.items():
ValueError: too many values to unpack (expected 2)
这是我的原始代码:
import nltk
import pandas as pd
import csv
import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import random
with open("/Users/mac/Documents/Transcribed.rtf", "rb") as f:
data = f.read().split()
random.shuffle(data)
train_data = data[:1200]
test_data = data[4800:]
classfier= nltk.NaiveBayesClassifier.train(train_data)
print(nltk.classify.accuracy(classifier, test_data))
答案 0 :(得分:0)
从我看到的问题出在第206行:
~/anaconda3/lib/python3.7/site-packages/nltk/classify/naivebayes.py in train(cls, labeled_featuresets, estimator)
204 # Count up how many times each feature value occurred, given
205 # the label and featurename.
--> 206 for featureset, label in labeled_featuresets:
207 label_freqdist[label] += 1
208 for fname, fval in featureset.items():
ValueError: too many values to unpack (expected 2)
该错误表明您在labeled_featuresets
中所期望的for featureset, label in labeled_featuresets
。因此,您要做的是打印labeled_featureset
的内容,并查看它的尺寸。