2017-12-12 8 views
0

OnevsRest 분류자를 사용하고 있습니다. 21 개 클래스로 구성된 데이터 세트가 있습니다. 각 분류 기준에 대한 정확성을 알고 싶습니다. 예를 들어OnevsRest 분류기를 사용할 때 각 분류기의 정확성을 알 수 있습니까?

(Class2의 classx + ... + class21) (classx 강좌 3 + ... + class21)

VS Class2의 대

정확도 대 Class1에 대한

정확도.

.

. (클래스 1 + classx ... + class20)

가 어떻게 그것을 알 수 대 class21에 대한

정확도?

# 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 클래스 × m 열)에 의해 classifier.coef_'. 또는'classifier.estimators_ : est_for_record_score (y_test, classifier.predict (X_test))'에서'for를 사용하십시오. 참고 : 처음에는 적합해야하고, 한 번에 점수를 받아야합니다. – Jarad

답변

0

이 기능은 기본적으로 지원되지 않으므로 직접 처리해야합니다.

여기에 예제 코드가 있습니다. 프로토 타입으로 테스트 할 것이므로! 단일 클래스 정확도와 메타 정확도 (Platt 스케일링으로 얻은 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