2017-12-12 6 views
0

私はOnevsRestクラシファイアを使用しています。 私は21のクラスを持つデータセットを持っています。各分類器の精度を知りたい。例えばOnevsRestクラシファイアを使用する場合、各クラシファイアの精度を知ることは可能ですか?

:(クラス2 + ... + classx class21)(Class3に+ classx ... + class21)

対class2のため

精度対Class1のため

精度。

。対class21のため

精度(クラス1の+のclassx ... + class20)

どのように私はそれを知ることができますか?

# Learn to predict each class against the other 
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True, random_state=random_state)) 
y_score = classifier.fit(X_train, y_train).score(X_test, y_test) 
print(y_score) 
+0

多分 '(nはクラスXのm列)によってclassifier.coef_'。または、classifier.estimators_:est_for_record_score(y_test、classifier.predict(X_test))で 'forを実行します。注:最初にフィットし、スコアを一気に得るだけでなく、フィットする必要があります。 – Jarad

答えて

0

私はこれがボックスからサポートされていないと思うので、あなた自身で行う必要があります。

ここにいくつかのコード例がありますが、それは非常にテストされていないのでプロトタイプと呼んでいます!単一クラスの精度とメタ精度(これは、確率推定によるもので、プラットスケーリングで得られたSVMの場合)と比較するのは難しいことに注意してください。

import numpy as np 
from sklearn import datasets 
from sklearn import svm 
from sklearn.multiclass import OneVsRestClassifier 
from sklearn.model_selection import train_test_split 

# Data 
iris = datasets.load_iris() 
iris_X = iris.data 
iris_y = iris.target 
X_train, X_test, y_train, y_test = train_test_split(
    iris_X, iris_y, test_size=0.5, random_state=0) 

# Train classifier 
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True, 
    random_state=0)) 
y_score = classifier.fit(X_train, y_train).score(X_test, y_test) 
print(y_score) 

# Get all accuracies 
classes = np.unique(y_train) 

def get_acc_single(clf, X_test, y_test, class_): 
    pos = np.where(y_test == class_)[0] 
    neg = np.where(y_test != class_)[0] 
    y_trans = np.empty(X_test.shape[0], dtype=bool) 
    y_trans[pos] = True 
    y_trans[neg] = False 
    return clf.score(X_test, y_trans)     # assumption: acc = default-scorer 

for class_index, est in enumerate(classifier.estimators_): 
    class_ = classes[class_index] 
    print('class ' + str(class_)) 
    print(get_acc_single(est, X_test, y_test, class_)) 

出力:

0.8133333333333334 
class 0 
1.0 
class 1 
0.6666666666666666 
class 2 
0.9733333333333334 
+0

'probability = False'を使用すると正確さはokeyになりますか? – Aizzaac

関連する問題