2013-10-28 3 views
7

私は次のコードのセットを使用していましたマルチラベルクラス以上の問題Pythonの:次のコードは、私の分類の中で私の作品 そして、私はX_trainの精度を確認する必要があるとX_test</p> <p>:どのようにSVMテキスト分類器アルゴリズムで精度の検索結果を見つけるためにマルチラベルクラス用

import numpy as np 
from sklearn.pipeline import Pipeline 
from sklearn.feature_extraction.text import CountVectorizer 
from sklearn.svm import LinearSVC 
from sklearn.feature_extraction.text import TfidfTransformer 
from sklearn.multiclass import OneVsRestClassifier 

X_train = np.array(["new york is a hell of a town", 
        "new york was originally dutch", 
        "the big apple is great", 
        "new york is also called the big apple", 
        "nyc is nice", 
        "people abbreviate new york city as nyc", 
        "the capital of great britain is london", 
        "london is in the uk", 
        "london is in england", 
        "london is in great britain", 
        "it rains a lot in london", 
        "london hosts the british museum", 
        "new york is great and so is london", 
        "i like london better than new york"]) 
y_train = [[0],[0],[0],[0] 
      ,[0],[0],[1],[1] 
      ,[1],[1],[1],[1] 
      ,[2],[2]] 
X_test = np.array(['nice day in nyc', 
        'the capital of great britain is london', 
        'i like london better than new york', 
        ]) 
target_names = ['Class 1', 'Class 2','Class 3'] 

classifier = Pipeline([ 
    ('vectorizer', CountVectorizer(min_df=1,max_df=2)), 
    ('tfidf', TfidfTransformer()), 
    ('clf', OneVsRestClassifier(LinearSVC()))]) 
classifier.fit(X_train, y_train) 
predicted = classifier.predict(X_test) 
for item, labels in zip(X_test, predicted): 
    print '%s => %s' % (item, ', '.join(target_names[x] for x in labels)) 

OUTPUT

nice day in nyc => Class 1 
the capital of great britain is london => Class 2 
i like london better than new york => Class 3 

トレーニングとテストデータセットの間の正確さをチェックしたいと思います。スコアはマルチラベル分類のためにサポートされていません

親切に私は、精度の結果を得るのを助ける: スコア関数は、私のために動作しません、それはNotImplementedErrorそのマルチラベル値を示すエラーが

>>> classifier.score(X_train, X_test) 

を受け入れたことはできません示していトレーニングデータをテストし、分類ケースのアルゴリズムを選択します。

答えて

9

テストセットの精度スコアを取得する場合は、y_testと呼ぶことができる回答キーを作成する必要があります。正しい答えがわからない限り、予測が正しいかどうかはわかりません。

回答キーを取得したら、正確さを得ることができます。あなたが望む方法はsklearn.metrics.accuracy_scoreです。

私は下にそれを書いている:

また
from sklearn.metrics import accuracy_score 

# ... everything else the same ... 

# create an answer key 
# I hope this is correct! 
y_test = [[1], [2], [3]] 

# same as yours... 
classifier.fit(X_train, y_train) 
predicted = classifier.predict(X_test) 

# get the accuracy 
print accuracy_score(y_test, predicted) 

、sklearnは精度のほかに、いくつかの他のメトリックを持っています。 sklearn.metrics

+0

ありがとう、それは私の問題のために働きます –

+2

「classification_report」(sklearnから)は、最も頻繁なメトリックを持つテーブルを含んでいるので、非常に便利です。 –

関連する問題