2012-09-19 52 views
0

我正在使用混淆矩陣來衡量分類器的性能。這個例子就很好地工作,我(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) 

對於打印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是相當扭曲的。另外,我應該如何繼續在矩陣中標記正方形?

回答

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空間選項卡。

+0

@ user1665514:好的,所以看起來我們看到了同樣的錯誤。你可以運行'python -tt yourprogramname.py'來檢查空白問題嗎? – DSM

+0

@ user1665514:您也可以在'plt.clf()'之前添加'print norm_conf'來查看它的值。 – DSM

+0

print norm_conf給出[] – user1665514