简体   繁体   English

热图中转置混乱矩阵的原因

[英]reason for transposed confusion matrix in heatmap

I plot a heatmap which takes a confusion matrix as input data. 我绘制了一个热图,该图以混淆矩阵作为输入数据。 The confusion matrix has the shape: 混淆矩阵的形状为:

 [[37  0  0  0  0  0  0  0  0  0]
 [ 0 42  0  0  0  1  0  0  0  0]
 [ 1  0 43  0  0  0  0  0  0  0]
 [ 0  0  0 44  0  0  0  0  1  0]
 [ 0  0  0  0 37  0  0  1  0  0]
 [ 0  0  0  0  0 47  0  0  0  1]
 [ 0  0  0  0  0  0 52  0  0  0]
 [ 0  0  0  0  1  0  0 47  0  0]
 [ 0  1  0  1  0  0  0  1 45  0]
 [ 0  0  0  0  0  2  0  0  0 45]]

The code to plot the heatmap is: 绘制热图的代码为:

fig2=plt.figure()
fig2.add_subplot(111)
sns.heatmap(confm.T,annot=True,square=True,cbar=False,fmt="d")
plt.xlabel("true label")
plt.ylabel("predicted label")

which yields: 产生:

在此处输入图片说明

As you can see, the input matrix "confm" is transposed (confm.T). 如您所见,输入矩阵“ confm”已转置(confm.T)。 What is the reason for this? 这是什么原因呢? Do I necessarily have to do that? 我一定要这样做吗?

When I plot your data with the code you provided I get this: 当我使用提供的代码绘制数据时,得到以下信息: 在此处输入图片说明

Without the transpose and when swapping the x and y labels you get: 没有转置,并且在交换x和y标签时,您将获得:

fig2=plt.figure()
fig2.add_subplot(111)
sns.heatmap(confm,annot=True,square=True,cbar=False,fmt="d")
plt.xlabel("predicted label")
plt.ylabel("true label")

在此处输入图片说明

Which results in the same confusion matrix. 这导致相同的混淆矩阵。 What the transpose really does is swap which is the prediction and which is the ground truth (true label). 换位的真正作用是交换,它是预测,这是基本事实(真实标签)。 What you need to use depends on how the data is formatted. 您需要使用什么取决于数据的格式。

You need to transpose only if you want to switch along which axis which data will be placed. 仅当您要沿哪个轴切换将放置哪些数据时,才需要进行转置。 I'm usually use confusion matrix as is: y - true labels, x - predicted labels. 我通常按​​如下方式使用混淆矩阵:y-真实标签,x-预测标签。 You need transpose matrix and swap labels only if you like it vice versa: y - predicted labels, x - true labels. 仅当您喜欢时,才需要转置矩阵和交换标签,反之亦然:y-预测标签,x-真实标签。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM