由已知的混淆矩阵数值重新画混淆矩阵

由已知的混淆矩阵数值重新画混淆矩阵

上一篇,写了怎么在YOLOv5代码中改混淆矩阵的“外貌”,这里我们并不局限于YOLOv5,正常的我得到一个丑的混淆矩阵,我想根据混淆矩阵上显示的数据重新画混淆矩阵,我应该怎么画呢?

先看效果:

画之前:
未调整过的混淆矩阵

画之后:
调整过的混淆矩阵

Python实现代码:

#confusion_matrix
import numpy as np
import matplotlib.pyplot as plt
# classes = ['A','B','C','D','E']
# confusion_matrix = np.array([(9,1,3,4,0),(2,13,1,3,4),(1,4,10,0,13),(3,1,1,17,0),(0,0,0,1,14)],dtype=np.float64)


# 标签
classes=['Local Lesion','Long Lesion','Bifurcation Lesion','CTO','background']#类别名称,注意除了需要的类,还有个背景,看自己想要不想要吧


# 标签的个数
classNamber=5 #类别的数量,和上面对应


# 在标签中的矩阵
confusion_matrix = np.array([
    (0.60,0.10,0.11,0.01,0.62),
    (0.05,0.79,0.00,0.03,0.21),
    (0.03,0.00,0.58,0.00,0.11),
    (0.00,0.00,0.00,0.77,0.06),
    (0.33,0.11,0.30,0.19,0.00),
    ],dtype=np.float64)#把混淆矩阵上面的数据输进来

# (0.54, 0.09, 0.07, 0.00, 0.66),
# (0.02, 0.81, 0.00, 0.02, 0.10),
# (0.01, 0.00, 0.66, 0.00, 0.21),
# (0.00, 0.00, 0.00, 0.47, 0.03),
# (0.43, 0.09, 0.27, 0.51, 0.00),
plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues)  #按照像素显示出矩阵
plt.title('Confusion Matrix',size=18)#size=18可以调整标题的字数大小
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=-270)#rotation=-270是旋转的角度
plt.yticks(tick_marks, classes)

thresh = confusion_matrix.max() / 2.
#iters = [[i,j] for i in range(len(classes)) for j in range((classes))]
#ij配对,遍历矩阵迭代器
iters = np.reshape([[[i,j] for j in range(classNamber)] for i in range(classNamber)],(confusion_matrix.size,2))
for i, j in iters:
    plt.text(j, i, format(confusion_matrix[i, j]),va='center',ha='center',size=16,color="black") #显示对应的数字color="black"是里面数字的颜色
plt.ylabel('Predicted',size=15)#注意你们的横纵坐标分别是预测还是真实值,同样size=15可以调整纵坐标标题的字大小
plt.xlabel('True',size=15)#同样size=15可以调整横坐标标题的字大小
plt.tight_layout()
plt.show()

plt.text

这里介绍一下Python画图plt的 text()方法的使用:
(1)text()方法:
plt.text(x, y, string, size, family, color, style, weight, verticalalignment, horizontalalignment, rotation, bbox=dict(facecolor, alpha, boxstyle))
x: 文本位置的横坐标
y:文本位置的纵坐标
string: 文本内容
size:文本字体大小
family:文本字体类型:比如宋体、Times new roman等
color:文本字体颜色
style:文本字体风格:italic(斜体)
weight:文本线条粗细:light
verticalalignment:垂直对齐方式 ,可选 ‘center’ ,‘top’ , ‘bottom’,‘baseline’ 等
horizontalalignment:水平对齐方式 ,可以填 ‘center’ , ‘right’ ,‘left’ 等
rotation:标签的旋转角度,以逆时针计算,取整
bbox:文本背景框设置:
( facecolor:背景颜色
alpha:背景颜色的透明度
boxstyle:背景框的风格(默认是直角):boxstyle=“round”(为圆角)
)
(2)案例设置:
plt.text(-3, 150, text1, size=12, family=“Times new roman”, color=“black”, style=‘italic’, weight = “light”, bbox=dict(facecolor=“dimgray”, alpha=0.5, boxstyle=“round”))

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值