簡體   English   中英

具有lxmxn廣播形狀的高級整數索引

[英]Advanced integer indexing with l x m x n broadcast shape

今天早些時候,我問有關整數數組索引的問題,我在獲取答案並將其應用於啟發問題的問題上遇到了麻煩。

簡而言之, p_stack1c_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

我檢查了aabb的形狀,它們都是(768, 1024)

我想念我什么?

看起來您混淆了尺寸:

c_stack1.shape   # (3, 768, 1024)
aa.max()         # 1023
bb.max()         # 767

所以,當你跑步

c_stack1[ind_new, aa, bb]

您將嘗試使用比可用值高的值來索引axis=1 ,因此出現錯誤

要么繞開aabb ,要么c_stack1[ind_new, bb, aa]也會解決問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM