python - 具有 lxmxn 广播形状的高级整数索引
问题描述
今天早些时候,我问过这个关于整数数组索引的问题,但我无法将答案应用到激发这个问题的问题上。
简而言之,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
我检查了and的形状,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]
也能解决问题
推荐阅读
- networking - kubernetes (rancher) 入口理解
- python - 开始按钮在我的 tkinter 程序中不起作用
- bosun - 从多个端点收集 Bosun 的数据
- javascript - progressBar.setVisibility(View.GONE); 有没有人请告诉我在停止加载后我可以在哪里使用 Progressbar
- java - 将 PDF 转换为 Word 策略
- javascript - 多个元素的 JS/Jquery 按钮处理程序
- azure - 停止数据工厂中的集成运行时
- reactjs - React:react-router-dom 的 Redirect 不是重新加载页面,而是更改了 URL
- python - pip 正在为 python2.7 安装,但不是为 python3.7
- xaml - 更改 barcolor Flyout xamarin xaml