SKlearn 中带有嵌套交叉验证的分类报告(Average/Individual 值)

Classification report with Nested Cross Validation in SKlearn (Average/Individual values)

是否可以通过某种解决方法从 cross_val_score 获取分类报告?我正在使用嵌套交叉验证,我可以在此处获得模型的各种分数,但是,我想查看外循环的分类报告。有什么建议吗?

# 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

我们可以定义自己的评分函数如下:

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

现在,只需使用我们的新评分函数调用 cross_val_score,使用 make_scorer

# 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 

它会同时将分类报告打印为文本return 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.

它只是对 Sandipan 的回答的补充,因为我无法对其进行编辑。如果我们想计算完整 运行 交叉验证的平均分类报告而不是单个折叠,我们可以使用以下代码:

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

#Make our customer score
def classification_report_with_accuracy_score(y_true, y_pred):
    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

为什么不选择最简单的路径!我会这样做 -

输入:

results = []
names = []
for name, model in models:
    print(name)
    for score in ["roc_auc", "f1", "precision", "recall", "accuracy"]:
        cvs = cross_val_score(model, train, target, scoring=score, cv=10).mean()
        print(score + " : "+ str(cvs))
       
    print('\n')
    
return names, results

输出: