【问题标题】:NLTK - Error getting classifier accuracyNLTK - 获取分类器准确性时出错
【发布时间】:2018-07-06 21:43:27
【问题描述】:

对 Python/NLTK 相当陌生,如果这是一个基本问题,请原谅我。

分类器似乎正在运行/工作正常,但在尝试通过 nltk.classify.accuracy 检索准确度时,我遇到了 ValueError

这是否与训练集包含在 [({xxx})] 中而测试集包含在 [xxx] 中有关?

错误状态:

results = classifier.classify_many([fs for (fs, l) in gold])
ValueError: too many values to unpack (expected 2)`

代码

 train = [('train', 'train'),
('next train in', 'train'),
('When is the next train', 'train'),
('How long until the next train', 'train'),
("Where is the next train", 'train'),
('dart', 'train'),
('next dart in', 'train'),
('When is the next dart', 'train'),
('How long until the next dart', 'train'),
("Where is the next dart", 'train'),
("Show me where", 'map'),
("Directions to", 'map'),
('map', 'map')]


all_words = set(word.lower() for passage in train for word in word_tokenize(passage[0]))
t = [({word: (word in word_tokenize(x[0])) for word in all_words}, x[1]) for x in train]
classifier = nltk.NaiveBayesClassifier.train(t)
classifier.show_most_informative_features()


test_sentence = 'Whatever my message is, hopefully something about trains'

test_sent_features = {word.lower(): (word in word_tokenize(test_sentence.lower())) for word in all_words}

print(classifier.classify(test_sent_features))
print(nltk.classify.accuracy(classifier, test_sent_features))

我确信我忽略了一些简单的东西,但我似乎无法发现它。感谢您对此的任何意见,谢谢。

【问题讨论】:

    标签: python nlp nltk


    【解决方案1】:

    是的,你做错了。想一想:除非给出答案,否则分类器模块将如何计算准确度?

    accuracy() 函数必须使用标记数据列表调用(“标签”是所需的分类),与调用train() 的方式相同。它需要一个完整的列表(而不仅仅是一个句子),以便它可以告诉你它计算出的答案中有多少是正确的。

    【讨论】:

      【解决方案2】:

      在 for 循环中使用 enumerate 函数。
      for index, item in enumerate(yourlist):

      【讨论】:

      • test_sent_features = {word.lower(): (word in word_tokenize(test_sentence.lower())) for word in enumerate(all_words)} 结果:AttributeError: 'tuple' object has no attribute 'lower' 你是这个意思吗?
      猜你喜欢
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 2016-06-16
      • 2014-11-24
      • 2018-04-11
      • 1970-01-01
      • 2013-04-22
      • 2021-12-10
      相关资源
      最近更新 更多