AttributeError:“列表"对象没有属性“复制" [英] AttributeError: 'list' object has no attribute 'copy'
问题描述
我有以下代码段
classifier = NaiveBayesClassifier.train(train_data)
#classifier.show_most_informative_features(n=20)
results = classifier.classify(test_data)
,错误显示在下一行
results = classifier.classify(test_data)
错误:
Traceback (most recent call last):
File "trial_trial.py", line 46, in <module>
results = classifier.classify(test_data)
File "c:\Users\Amr\Anaconda\lib\site-packages\nltk\classify\naivebayes.py", line 88, in classify
return self.prob_classify(featureset).max()
File "c:\Users\Amr\Anaconda\lib\site-packages\nltk\classify\naivebayes.py", line 94, in prob_classify
featureset = featureset.copy()
AttributeError: 'list' object has no attribute 'copy'
我想到了在python中扩展基类列表并添加复制功能 但是我不是python专家,也不知道如何解决这个问题.
I think of extending base class list in python and add copy function but I'm not expert in python and I don't know how to solve this problem.
推荐答案
NLTK分类器与功能集一起使用;这些总是作为字典给出,其特征名称映射到一个值.相反,您传递的是列表,因此不会按照NLTK文档的要求生成功能.该代码只需要一个Python字典,而Python字典具有一个.copy()
方法.
NLTK classifiers work with feature sets; these are always given as dictionaries with feature names mapping to a value. You are passing in a list instead, so you are not producing features as per the NLTK documentation. The code simply expects a Python dictionary, and Python dictionaries have a .copy()
method.
请参阅关于学习文本分类 :
See the NLTK tutorial chapter on Learning to Classify Text:
返回的字典,称为功能集,从功能名称映射到其值.特征名称是区分大小写的字符串,通常提供对特征的简短人类可读描述,例如示例
'last_letter'
.特征值是具有简单类型的值,例如布尔值,数字和字符串.
The returned dictionary, known as a feature set, maps from feature names to their values. Feature names are case-sensitive strings that typically provide a short human-readable description of the feature, as in the example
'last_letter'
. Feature values are values with simple types, such as booleans, numbers, and strings.
另请参阅NLTK分类的 功能集部分API文档:
Also see the Featuresets section of the NLTK Classify API documentation:
使用功能集"对描述令牌的功能进行编码,该功能集是从功能名称"映射到功能值"的字典.功能名称是唯一的字符串,用于指示功能对令牌的哪个方面进行编码.
The features describing a token are encoded using a "featureset", which is a dictionary that maps from "feature names" to "feature values". Feature names are unique strings that indicate what aspect of the token is encoded by the feature.
您尚未共享train_data
列表包含的对象类型.如果这些是功能集词典,则您应该使用classify_many()
代替:
You haven't shared what kind of objects the train_data
list contains; if those are feature set dictionaries, you want to use classify_many()
instead:
results = classifier.classify_many(test_data)
该方法确实带有一个列表,但是每个元素仍然必须是有效的功能集.
That method does take a list, but each element must still be a valid feature set.
这篇关于AttributeError:“列表"对象没有属性“复制"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!