首页 > 解决方案 > 我正在尝试使用 NLTK 朴素贝叶斯分类器对文本进行分类。我收到 ValueError: too many values to unpack (expected 2)

问题描述

我对文本进行了清理并生成了二元语法。正在显示二元组,但我正在尝试训练和测试文本以使用 NLTK 朴素贝叶斯进行分类。我收到标题中显示的错误。

import nltk
from nltk.util import ngrams
#generating bigrams for all narratives
bigrams_all=ngrams(df,2)
#printing bigrams of one narrative
ninety_seven=df.loc[97].loc['FSR Narrative']
nine_bi=ngrams(ninety_seven,2)
print(nine_bi)
print([" ".join(t) for t in nine_bi])

# set that we'll train our classifier with
training_set = df[:1280]

# set that we'll test our classifier with
training_set = df[1280:]

classifier = nltk.NaiveBayesClassifier.train(training_set)

错误跟踪:

ValueError                                Traceback (most recent call last)
<ipython-input-13-745201c14989> in <module>()
    113 training_set = df[1280:]
    114 
--> 115 classifier = nltk.NaiveBayesClassifier.train(training_set)

C:\Anaconda\envs\py35\lib\site-packages\nltk\classify\naivebayes.py in train(cls, labeled_featuresets, estimator)
    195         # Count up how many times each feature value occurred, given
    196         # the label and featurename.
--> 197         for featureset, label in labeled_featuresets:
    198             label_freqdist[label] += 1
    199             for fname, fval in featureset.items():

ValueError: too many values to unpack (expected 2)

标签: python-3.x

解决方案


推荐阅读