【问题标题】:Confusion Matrix Error: Invalid dimensions for image data混淆矩阵错误:图像数据的尺寸无效
【发布时间】:2012-09-11 18:32:12
【问题描述】:

我正在使用混淆矩阵来衡量我的分类器的性能。这个例子对我来说很好用 (its from here),但我得到了整个时间 TypeError: Invalid dimensions for image data

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

conf_arr = [[50.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [3.0, 26.0, 0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0], [4.0, 1.0, 0.0, 5.0, 0.0, 0.0, 0.0], [3.0, 0.0, 1.0, 0.0, 6.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 47.0, 0.0], [2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 8.0]]

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")

我是 python 和 matplotlib 的新手。有什么帮助吗?

Matplot 版本为 1.1.1。这是完整的回溯:

在 res =... 之后我得到了

TypeError      Traceback (most recent call last)
    C:\Python27\lib\site-packages\SimpleCV\Shell\Shell.pyc in <module>()
    ----> 1 res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')

C:\Python27\lib\site-packages\matplotlib\axes.pyc in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filter
rad, imlim, resample, url, **kwargs)
   6794                        filterrad=filterrad, resample=resample, **kwargs)
   6795
-> 6796         im.set_data(X)
   6797         im.set_alpha(alpha)
   6798         self._set_artist_props(im)

C:\Python27\lib\site-packages\matplotlib\image.pyc in set_data(self, A)
    409         if (self._A.ndim not in (2, 3) or
    410             (self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
--> 411             raise TypeError("Invalid dimensions for image data")
    412
    413         self._imcache =None

TypeError: Invalid dimensions for image data

SimpleCV:105> cb = fig.colorbar(res)

对于 print norm_conf,我现在得到结果:[[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],...]]。我纠正了缩进问题。但我的困惑 .png 非常扭曲。进一步我应该如何继续标记矩阵中的正方形?

【问题讨论】:

    标签: python matplotlib confusion-matrix


    【解决方案1】:

    这对我来说很好(matplotlib 1.1.1rc)。最初我希望你确认你的 matplotlib 版本并发布整个回溯——“回溯”是指 TypeError 行之前的几行,它们显示了导致错误的原因——这仍然是一个好主意,但我想我明白了什么问题可能是。

    如果norm_conf 以某种方式没有被填充(即norm_conf = []),这就是你会得到的错误:

    Traceback (most recent call last):
      File "mdim2.py", line 19, in <module>
        res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')
      File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 6796, in imshow
        im.set_data(X)
      File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 411, in set_data
        raise TypeError("Invalid dimensions for image data")
    TypeError: Invalid dimensions for image data
    

    您的代码看起来可能存在一些缩进问题,这在使用混合制表符和空格时经常发生。因此,我建议 (1) 尝试 python -tt yourprogramname.py 以查看是否存在空白错误,以及 (2) 确保您始终使用 4 空格制表符。

    【讨论】:

    • @user1665514:好的,所以看起来我们看到了同样的错误。你能运行python -tt yourprogramname.py 来检查空格问题吗?
    • @user1665514:您还可以在plt.clf() 之前添加print norm_conf 以查看其值。
    • @user1665514:是的,那就是问题所在。我能想到 norm_conf 没有被值填充的唯一原因是,由于空格问题,该循环没有被执行。
    猜你喜欢
    • 2014-04-17
    • 1970-01-01
    • 2019-10-15
    • 2015-09-18
    • 1970-01-01
    • 2019-07-06
    • 2018-11-22
    • 2020-02-23
    • 2020-03-09
    相关资源
    最近更新 更多