将3D numpy数组的参数传递给4D numpy数组

时间:2017-12-14 15:02:14

标签: python python-3.x numpy numpy-broadcasting

我想通过在该数组的简化3D版本上评估的numpy.argsort的结果对4D numpy数组进行排序。像这样:

array.shape
    (7, 3178, 3178, 3)
array_reduced.shape
    (7, 3178, 3178)
args=numpy.argsort(array_reduced,axis=0)
array_sorted=array[args,:]

这会返回内存错误:

    ---------------------------------------------------------------------------
    MemoryError                               Traceback (most recent call last)
    <ipython-input-48-04f432d9e05d> in <module>()
          ----> 1 array_sorted=array[args,:]

    MemoryError:

这可能是一个关于如何施放lambda函数的愚蠢错误,但如果有人可以帮助我,我会非常感激!

--------------------------------------编辑-------- ---------------

这段代码完成了我想要它做的事情,但它很慢:

array_sorted=np.zeros(array.shape,dtype=np.uint8)
for thet in range (0, array.shape[0]):
    print(thet)
    for y in range (0, array.shape[1]):
        for x in range (0, array.shape[2]):
            array_sorted[thet,y,x,0]=(array[args[thet,y,x],y,x,0])
            array_sorted[thet,y,x,1]=(array[args[thet,y,x],y,x,1])
            array_sorted[thet,y,x,2]=(array[args[thet,y,x],y,x,2])

1 个答案:

答案 0 :(得分:0)

您可以使用np.ogrid

对您的循环进行矢量化
i,j,k = np.ogrid[tuple(map(slice, array.shape[:-1]))]
array_sorted = array[args, j, k]