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) 

ここでは、スコアレポートの横に分類レポートが表示されます。 http://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html

答えて

4

当社は、以下のように私たち自身のスコア関数を定義することができます。

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 

を今、ちょうどmake_scorerを使用して、私たちの新しいスコアリング機能でcross_val_scoreを呼び出す:

# 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たとえば、この新しいスコアリング機能を実行すると、以下のように、出力の最後の数行は次のようになります。私はそれを編集できなかったとしてSandipanの答えにちょうど加え

# 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

その。

# 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 
関連する問題