[英]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.