gpt4 book ai didi

python - 带有分类/错误分类实例数量的混淆矩阵(Python/Matplotlib)

转载 作者:太空狗 更新时间:2023-10-29 18:29:14 25 4
gpt4 key购买 nike

我正在使用 matplotlib 使用以下代码绘制混淆矩阵:

from numpy import *
import matplotlib.pyplot as plt
from pylab import *

conf_arr = [[33,2,0,0,0,0,0,0,0,1,3], [3,31,0,0,0,0,0,0,0,0,0], [0,4,41,0,0,0,0,0,0,0,1], [0,1,0,30,0,6,0,0,0,0,1], [0,0,0,0,38,10,0,0,0,0,0], [0,0,0,3,1,39,0,0,0,0,4], [0,2,2,0,4,1,31,0,0,0,2], [0,1,0,0,0,0,0,36,0,2,0], [0,0,0,0,0,0,1,5,37,5,1], [3,0,0,0,0,0,0,0,0,39,0], [0,0,0,0,0,0,0,0,0,0,38] ]

norm_conf = []
for i in conf_arr:
a = 0
tmp_arr = []
a = sum(i,0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)

plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111)
res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')
cb = fig.colorbar(res)
savefig("confmat.png", format="png")

但我想让混淆矩阵像这张图(右图)一样显示上面的数字。如何在图形上绘制 conf_arr

confusion matrix

最佳答案

您可以使用 text在你的情节中放置任意文本。例如,将以下行插入到您的代码中将写入数字(请注意,第一行和最后一行来自您的代码,以显示在何处插入我的行):

res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')
for i, cas in enumerate(conf_arr):
for j, c in enumerate(cas):
if c>0:
plt.text(j-.2, i+.2, c, fontsize=14)
cb = fig.colorbar(res)

matrix with numbers

关于python - 带有分类/错误分类实例数量的混淆矩阵(Python/Matplotlib),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2897826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com