如何显示混淆矩阵和每个交叉验证折叠的报告(召回率,精度,fmeasure) [英] How to display confusion matrix and report (recall, precision, fmeasure) for each cross validation fold

查看:120
本文介绍了如何显示混淆矩阵和每个交叉验证折叠的报告(召回率,精度,fmeasure)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python中执行10折交叉验证.我知道如何计算混淆矩阵和分割测试报告(例如分割80%训练和20%测试).但是问题是我不知道如何计算混淆矩阵并为每个折页生成报告,例如当10折时,我只知道平均准确度的代码.

I am trying to perform 10 fold cross validation in python. I know how to calculate the confusion matrix and the report for split test(example split 80% training and 20% testing). But the problem is I don't know how to calculate the confusion matrix and report for each folds for example when fold-10, I just know code for average accuracy.

推荐答案

为简单起见,这里有一个可复制的示例,其中包含乳腺癌数据和3倍CV:

Here is a reproducible example with the breast cancer data and 3-fold CV for simplicity:

from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.model_selection import KFold

X, y = load_breast_cancer(return_X_y=True)
n_splits = 3
kf = KFold(n_splits=n_splits, shuffle=True)
model = DecisionTreeClassifier()

for train_index, val_index in kf.split(X):
    model.fit(X[train_index], y[train_index])
    pred = model.predict(X[val_index])
    print(confusion_matrix(y[val_index], pred))
    print(classification_report(y[val_index], pred))

结果是3个混淆矩阵& ;;分类报告,每CV折叠一份:

The result is 3 confusion matrices & classification reports, one per CV fold:

[[ 63   9]
 [ 10 108]]
              precision    recall  f1-score   support

           0       0.86      0.88      0.87        72
           1       0.92      0.92      0.92       118

   micro avg       0.90      0.90      0.90       190
   macro avg       0.89      0.90      0.89       190
weighted avg       0.90      0.90      0.90       190

[[ 66   8]
 [  6 110]]
              precision    recall  f1-score   support

           0       0.92      0.89      0.90        74
           1       0.93      0.95      0.94       116

   micro avg       0.93      0.93      0.93       190
   macro avg       0.92      0.92      0.92       190
weighted avg       0.93      0.93      0.93       190

[[ 59   7]
 [  8 115]]
              precision    recall  f1-score   support

           0       0.88      0.89      0.89        66
           1       0.94      0.93      0.94       123

   micro avg       0.92      0.92      0.92       189
   macro avg       0.91      0.91      0.91       189
weighted avg       0.92      0.92      0.92       189

这篇关于如何显示混淆矩阵和每个交叉验证折叠的报告(召回率,精度,fmeasure)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆