이 기능은 기본적으로 지원되지 않으므로 직접 처리해야합니다.
여기에 예제 코드가 있습니다. 프로토 타입으로 테스트 할 것이므로! 단일 클래스 정확도와 메타 정확도 (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
아마 '(N 클래스 × m 열)에 의해 classifier.coef_'. 또는'classifier.estimators_ : est_for_record_score (y_test, classifier.predict (X_test))'에서'for를 사용하십시오. 참고 : 처음에는 적합해야하고, 한 번에 점수를 받아야합니다. – Jarad