具有l x m x n广播形状的高级整数索引

时间:2018-08-22 16:28:23

标签: python arrays numpy indexing

今天早些时候,我向this询问了整数数组索引的问题,但我很难理解答案并将其应用于启发问题的问题。

简而言之,p_stack1c_stack1包含从我正在研究的图像处理算法派生的数组。 p_stack1包含概率数据,c_stack1包含整数分类。我需要找到尺寸为768 x 1024的图像中每个像素的概率最高的分类。从integer array indexing的文档中,它提供了一种使用其整数索引对较高维数组的数据进行子集化的方法。 / p>

我最初的问题的解决方案适用于n x n x n形状阵列的简化示例,但似乎不适用于l x m x n形状阵列。

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

我想念的是什么?

1 个答案:

答案 0 :(得分:2)

好像您混淆了尺寸:

column

所以,当您运行

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

您将尝试使用比可用值更高的值来索引c_stack1[ind_new, aa, bb] ,因此出现错误

要么绕过axis=1aa,否则bb也能解决问题