[英]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.