Seaborn Confusion Matrix(热图)2种配色方案(正确的对角线与错误的其余部分) [英] Seaborn Confusion Matrix (heatmap) 2 color schemes (correct diagonal vs wrong rest)

查看:770
本文介绍了Seaborn Confusion Matrix(热图)2种配色方案(正确的对角线与错误的其余部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在混淆矩阵中,对角线表示预测标签与正确标签匹配的情况.因此对角线是好的,而其他所有单元格都是坏的.为了弄清楚对非专家而言,CM的优点和缺点,我想为对角线提供与其余颜色不同的颜色.我想使用 Python& Seaborn .

In a confusion matrix, the diagonal represents the cases that the predicted label matches the correct label. So the diagonal is good, while all other cells are bad. To clarify what is good and what is bad in a CM for non-experts, I want to give the diagonal a different color than the rest. I want to achieve this with Python & Seaborn.

基本上,我正在尝试实现此问题在R中的作用(

Basically I'm trying to achieve what this question does in R (ggplot2 Heatmap 2 Different Color Schemes - Confusion Matrix: Matches in Different Color Scheme than Missclassifications)

import numpy as np
import seaborn as sns

cf_matrix = np.array([[50, 2, 38],
                      [7, 43, 32],
                      [9,  4, 76]])

sns.heatmap(cf_matrix, annot=True, cmap='Blues')  # cmap='OrRd'

这张图片的结果是

我想用例如cmap='OrRd'.所以我想象会有2个颜色条,对角线1个蓝色,其他单元格1个.最好两个颜色条的值都匹配(因此例如0-70而不是0-70和0-40). 我将如何处理?

I would like to color the non-diagonal cells with e.g. cmap='OrRd'. So I imagine there would be 2 colorbars, 1 blue for the diagonal and 1 for the other cells. Preferably the values of both colorbars match (so both e.g. 0-70 and not 0-70 and 0-40). How would I approach this?

以下内容不是使用代码,而是使用照片编辑软件进行的:

The following is not made with code, but with photo editing software:

推荐答案

您可以在对heatmap()的调用中使用mask=选择要显示的单元格.对角线和off_diagonal单元使用两个不同的蒙版,可以获得所需的输出:

You can use mask= in the call to heatmap() to choose which cells to show. Using two different masks for the diagonal and the off_diagonal cells, you can get the desired output:

import numpy as np
import seaborn as sns

cf_matrix = np.array([[50, 2, 38],
                      [7, 43, 32],
                      [9,  4, 76]])

vmin = np.min(cf_matrix)
vmax = np.max(cf_matrix)
off_diag_mask = np.eye(*cf_matrix.shape, dtype=bool)

fig = plt.figure()
sns.heatmap(cf_matrix, annot=True, mask=~off_diag_mask, cmap='Blues', vmin=vmin, vmax=vmax)
sns.heatmap(cf_matrix, annot=True, mask=off_diag_mask, cmap='OrRd', vmin=vmin, vmax=vmax, cbar_kws=dict(ticks=[]))

如果想花哨的话,可以使用GridSpec创建轴以具有更好的布局:

If you want to get fancy, you can create the axes using GridSpec to have a better layout:

将numpy导入为np 将seaborn导入为sns

import numpy as np import seaborn as sns

fig = plt.figure()
gs0 = matplotlib.gridspec.GridSpec(1,2, width_ratios=[20,2], hspace=0.05)
gs00 = matplotlib.gridspec.GridSpecFromSubplotSpec(1,2, subplot_spec=gs0[1], hspace=0)

ax = fig.add_subplot(gs0[0])
cax1 = fig.add_subplot(gs00[0])
cax2 = fig.add_subplot(gs00[1])

sns.heatmap(cf_matrix, annot=True, mask=~off_diag_mask, cmap='Blues', vmin=vmin, vmax=vmax, ax=ax, cbar_ax=cax2)
sns.heatmap(cf_matrix, annot=True, mask=off_diag_mask, cmap='OrRd', vmin=vmin, vmax=vmax, ax=ax, cbar_ax=cax1, cbar_kws=dict(ticks=[]))

这篇关于Seaborn Confusion Matrix(热图)2种配色方案(正确的对角线与错误的其余部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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