3

cross_val_score에서 분류 방법 보고서를 얻을 수 있습니까? 중첩 된 교차 유효성 검사를 사용하고 있으며 모델에 대해 다양한 점수를 얻을 수 있지만 바깥 쪽 루프의 분류 보고서를보고 싶습니다. 어떤 추천?SKlearn의 중첩 된 교차 유효성 검사가 포함 된 분류 보고서

# Choose cross-validation techniques for the inner and outer loops, 
# independently of the dataset. 
# E.g "LabelKFold", "LeaveOneOut", "LeaveOneLabelOut", etc. 
inner_cv = KFold(n_splits=4, shuffle=True, random_state=i) 
outer_cv = KFold(n_splits=4, shuffle=True, random_state=i) 

# Non_nested parameter search and scoring 
clf = GridSearchCV(estimator=svr, param_grid=p_grid, cv=inner_cv) 

# Nested CV with parameter optimization 
nested_score = cross_val_score(clf, X=X_iris, y=y_iris, cv=outer_cv) 

여기 점수 점수 옆에있는 분류 보고서를보고 싶습니다. make_scorer를 사용하여, 우리의 새로운 채점 기능 cross_val_score를 호출, 지금

from sklearn.metrics import classification_report, accuracy_score, make_scorer 

def classification_report_with_accuracy_score(y_true, y_pred): 

    print classification_report(y_true, y_pred) # print classification report 
    return accuracy_score(y_true, y_pred) # return accuracy score 

을 : http://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html

답변

4

우리는 다음과 같이 우리 자신의 스코어링 함수를 정의 할 수 있습니다

# Nested CV with parameter optimization 
nested_score = cross_val_score(clf, X=X_iris, y=y_iris, cv=outer_cv, \ 
       scoring=make_scorer(classification_report_with_accuracy_score)) 
print nested_score 

이 분류 보고서를 인쇄합니다 동시에 텍스트로 nested_score을 숫자로 반환하십시오. 이 새로운 채점 기능을 실행하면 다음과 같이

http://scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html 예는 출력의 마지막 몇 줄은 다음과 같습니다

# precision recall f1-score support  
#0  1.00  1.00  1.00  14 
#1  1.00  1.00  1.00  14 
#2  1.00  1.00  1.00   9 

#avg/total  1.00  1.00  1.00  37 

#[ 0.94736842 1.   0.97297297 1. ] 

#Average difference of 0.007742 with std. dev. of 0.007688. 
+1

감사합니다. 그것은 나에게 개인 폴드에 대한 분류 보고서를 제공하지만 평균을 내겠다. – utengr

+1

다른 사람들이 사용하기를 원한다면 평균 분류 보고서에 대한 코드를 추가 할 것입니다. – utengr

2

그 Sandipan의 대답에 그냥 추가 내가 그것을 편집 할 수있다.

# Variables for average classification report 
originalclass = [] 
predictedclass = [] 

#Make our customer score 
def classification_report_with_accuracy_score(y_true, y_pred): 
#print(classification_report(y_true, y_pred)) # print classification report 
originalclass.extend(y_true) 
predictedclass.extend(y_pred) 
return accuracy_score(y_true, y_pred) # return accuracy score 

inner_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=i) 
outer_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=i) 

# Non_nested parameter search and scoring 
clf = GridSearchCV(estimator=svr, param_grid=p_grid, cv=inner_cv) 

# Nested CV with parameter optimization 
nested_score = cross_val_score(clf, X=X_iris, y=y_iris, cv=outer_cv, scoring=make_scorer(classification_report_with_accuracy_score)) 

# Average values in classification report for all folds in a K-fold Cross-validation 
print(classification_report(originalclass, predictedclass)) 

지금과 같을 것이다 Sandipan의 대답에, 예를 들어 결과 : 우리가 대신 각각의 주름의 교차 검증의 전체 실행에 대한 평균 분류 보고서를 계산하려면, 우리는 다음과 같은 코드를 사용할 수 있습니다 :

  precision recall f1-score support 

      0  1.00  1.00  1.00  50 
      1  0.96  0.94  0.95  50 
      2  0.94  0.96  0.95  50 

avg/total  0.97  0.97  0.97  150