2016-05-05 34 views
0

を持っていない私は、次のコードスニペットはAttributeError: 'リスト' オブジェクトが属性 'コピー'

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で基本クラスのリストを拡張し、コピー機能を追加すると思います しかし、私はpytの専門家ではありません私はこの問題を解決する方法を知らない。

+0

は、あなたが使用している機能のマニュアルをチェックし、正しい引数型を渡していることを確認してください。 – user2357112

+1

おそらく、 'train_data'はリストではなく*辞書*でなければなりません。 –

+0

これは分類のソースです http://www.nltk.org/_modules/nltk/classify/naivebayes.html –

答えて

2

NLTK分類器はと機能します。;これらは常に、フィーチャー名が値にマッピングされた辞書として与えられます。代わりにリストを渡しているので、NLTKのドキュメントに従って機能を作成していません。コードは単にPython辞書を予期し、Python辞書は.copy()メソッドを持っています。

NLTK tutorial chapter on Learning to Classify Textを参照してください:

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.

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()を使用したい:を行い

results = classifier.classify_many(test_data) 

このメソッドリストを取るが、各要素がまだ有効な機能セットでなければなりません。

+0

私は質問を投稿しました。ドキュメントを読み込んで辞書の1つの要素を辞書に分類して作業した後に、私はclassify_many()を試みます –

-1

なぜそれをしたいですか? あなたはそれを行うことができ、別のに等しいのリストを設定したい場合:

a = ['a','b','c'] 
b = ['d','e','f'] 
for i in len(a): 
    b.append(i-1) 
+0

[リストをPythonで複製またはコピーするには?](http://stackoverflow.com/q/2612802);コピーではなく、最初の例で追加の* reference *を作成するだけです。この特定のケースでは、OPは辞書を処理するAPIに間違ったデータ型を渡すだけです。 –

1

list.copy:あなたはAPPEND()を使用することができますsecondone内部のリストを挿入したい場合は、他の

a = ['a', 'b', 'c'] 
b = a 

をメソッドがPython 2.xとPython 3.xの両方で動作しない場合、なぜそれがドキュメントに残っているのだろうかと思います。 リストをコピーし、ユーザーリストキーワードの結果を達成するために:

fruits = ['banana', 'cucumber', 'apple', 'water mellon'] 
my_fruits = list(fruits) 

オプションを、あなたがそれをスライスして、リストをコピーすることができます。

my_fruits_copy = fruits[:] 
関連する問題