简体   繁体   English

在sklearn指标confusion_matrix中包括零命中的行和列

[英]Include rows and columns with zero hits in sklearn metrics confusion_matrix

I want to combine results from several confusion matrixes in a single matrix.我想将多个混淆矩阵的结果组合在一个矩阵中。 My prediction and ground truth values can be 0, 1, 2. I can add up values whenever all values are present.我的预测值和基本事实值可以是 0、1、2。只要所有值都存在,我就可以将值相加。 However the number of rows and columns changes when some values are not present at all.但是,当某些值根本不存在时,行数和列数会发生变化。 Hence, I cannot add such array to the resulting array.因此,我不能将这样的数组添加到结果数组中。 How to force confusion matrix include rows and columns with zero samples in them?如何强制混淆矩阵包括其中零样本的行和列?

import numpy as np
from sklearn.metrics import confusion_matrix

combined_confusion_matrix = np.zeros((3,3))
gt1 = np.array([0,1,2,0,1,2])
pred1 = np.array([2,1,2,0,1,2])
cm1 = confusion_matrix(gt1, pred1)  # [[1 0 1][0 2 0][0 0 2]]
combined_confusion_matrix +=  cm1
gt2 = np.array([0,0,2,2])
pred2 = np.array([0,2,2,2])
cm2 = confusion_matrix(gt2, pred2)  # Got [[1 1][0 2]], Desired [[1 0 1][0 0 0][0 0 2]]
combined_confusion_matrix += cm2  # Error due to different dimensions

This worked for me:这对我有用:

my_bins = [0, 1, 2]
cm2 = confusion_matrix(gt2, pred2, labels=my_bins)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于 sklearn 中的 confusion_matrix() - About confusion_matrix() in sklearn 为什么我的 sklearn.metrics chaos_matrix output 看起来是转置的? - Why does my sklearn.metrics confusion_matrix output look transposed? 更改混淆矩阵(sklearn)中的值 - Changing values in confusion_matrix (sklearn) 如何从函数内打印sklearn confusion_matrix输出? - How to print sklearn confusion_matrix output from within a function? 使用 sklearn SVC 计算训练集的混淆矩阵 - Calculate confusion_matrix for Training set with sklearn SVC sklearn confusion_matrix: ValueError: 没有足够的值来解压(预期 4,得到 1) - sklearn confusion_matrix: ValueError: not enough values to unpack (expected 4, got 1) 为什么我的 sklearn 混淆矩阵和 plot_confusion_matrix 的值不相等? - Why my values from sklearn confusion_matrix and plot_confusion_matrix are not equal? sklearn混淆_矩阵在错误的位置显示错误的尺寸/刻度线 - sklearn confusion_matrix displaying with wrong dimensions / tick marks at wrong spots Confusion_matrix ValueError:分类指标无法处理二进制和连续多输出目标的混合 - Confusion_matrix ValueError: Classification metrics can't handle a mix of binary and continuous-multioutput targets 如何解释sklearn confusion_matrix函数中的labels参数? - How do I interpret the labels argument in the sklearn confusion_matrix function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM