代码之家  ›  专栏  ›  技术社区  ›  dubbbdan

l×m×n广播形状的高级整数索引

  •  0
  • dubbbdan  · 技术社区  · 6 年前

    今天早些时候我问 this 关于整数数组索引,我很难找到答案并将其应用到激发问题的问题上。

    p_stack1 c_stack1 堆垛1 包含概率数据和 c U堆叠1 包含整数分类。我需要找到在768x 1024尺寸的图像中每个像素的概率最高的分类。从文件中 integer array indexing

    我最初问题的解决方案适用于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])
    

    最后一个print语句返回错误:

    IndexError: index 768 is out of bounds for axis 1 with size 768
    

    aa bb (768, 1024)

    我错过了什么?

    1 回复  |  直到 6 年前
        1
  •  2
  •   KenHBS    6 年前

    看起来你把尺寸搞混了:

    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] 也会成功的