使用边界检查索引 numpy 数组

Indexing numpy array with bounds checking

假设我有一些随机的 numpy 数组:

>>> x = np.random.randn(10, 4, 4)

所以这可以看作是 10 个 4x4 数组。现在,我还有一些关于这些数组的坐标:

>>> coords.shape
(10, 4, 4, 2)

此数组的特定元素可能如下所示:

>>> coords[i][j][k]
array([ 2,  2])

现在,我想创建一个数组,y,大小为 (10, 4, 4),其中 y[i][j][k] = x[coords[i][j][k]] 如果 coords[i][j][k] 在边界内,0 否则。

我尝试过如下操作:

>>> y = np.where(np.logical_and(
                 np.all(coords >= 0, axis = 3), 
                 np.all(coords <= 3, axis = 3)), 
                 x[tuple(coords)], 0)

但我无法完全正确地设置广播规则,并且出现错误。

例如,假设我们有,

>>> x = np.array([
                 [[1., 2.],
                  [4., 5.]],
                 [[6., 7.],
                  [8., 9.]]])

>>> coords = np.array([
                       [[[0,-1], [0, 0]],
                        [[1, 1], [1, 0]]],
                       [[[1, 1], [1, 0]],
                        [[7, 2], [0, 0]]]
                      ])

然后,我想以某种方式得到

y = np.array([
             [[0., 1.],
              [5., 4.]],
             [[9., 8.],
              [0., 6.]]]) 

如何在 Python 中实现此目的?

似乎需要一些 adavnced-indexing 的工作,应该适用于一般 n-dim 个案例 -

m,n = x.shape[0], x.shape[-1]
mask = (coords < n).all((-1)) & (coords >=0).all((-1))
v_coords = np.where(mask[:,:,:,None],coords,0)
out = x[np.arange(m)[:,None,None], v_coords[...,0], v_coords[...,1]]
out[~mask] = 0