[英]shape mismatch: value array of shape X could not be broadcast to indexing result of shape . . . X?
[英]Advanced integer indexing with l x m x n broadcast shape
今天早些時候,我問了有關整數數組索引的問題,我在獲取答案並將其應用於啟發問題的問題上遇到了麻煩。
簡而言之, p_stack1
和c_stack1
包含從我正在處理的圖像處理算法派生的數組。 p_stack1
包含概率數據,而c_stack1
包含整數分類。 我需要找到尺寸為768 x 1024的圖像中每個像素的概率最高的分類。從用於整數數組索引的文檔中,它提供了一種使用其整數索引對較高維數組的數據進行子集化的方法。
我最初提出的問題的解決方案適用於nxnxn形狀的數組的簡化示例,但似乎不適用於lxmxn形狀的數組。
#dummy data
p_stack1 = np.reshape(np.random.uniform(0,1,2359296),(3,768,1024))
c_stack1 = np.reshape(np.random.randint(0,4,2359296),(3,768,1024))
#find where max value occurs on axis 0
ind_new=p_stack1.argmax(axis=0)
#Create assending indicies
nx, ny = 768,1024
xx = np.arange(ny)
aa= np.tile(xx,(ny,1))
bb = np.column_stack(tuple(aa))[:nx,:]
aa= np.tile(xx,(ny,1))[:nx,:]
#perform the integer array indexing
print(c_stack1[ind_new, aa,bb])
最后的打印語句返回錯誤:
IndexError: index 768 is out of bounds for axis 1 with size 768
我檢查了aa
和bb
的形狀,它們都是(768, 1024)
我想念我什么?
看起來您混淆了尺寸:
c_stack1.shape # (3, 768, 1024)
aa.max() # 1023
bb.max() # 767
所以,當你跑步
c_stack1[ind_new, aa, bb]
您將嘗試使用比可用值高的值來索引axis=1
,因此出現錯誤
要么繞開aa
和bb
,要么c_stack1[ind_new, bb, aa]
也會解決問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.