NumPy中具有默认值的N-D索引

y0u0uwnf  于 2023-01-30  发布在  其他
关注(0)|答案(3)|浏览(139)

是否可以对NumPy N-D数组进行索引,并在超出范围时回退到默认值?下面是一些假想np.get_with_default(a, indexes, default)的示例代码:

import numpy as np
print(np.get_with_default(
    np.array([[1,2,3],[4,5,6]]), # N-D array
    [(np.array([0, 0, 1, 1, 2, 2]), np.array([1, 2, 2, 3, 3, 5]))], # N-tuple of indexes along each axis
    13, # Default for out-of-bounds fallback
))

肩印

[2 3 6 13 13 13]

我正在寻找一些内置的功能。如果这样的不存在,那么至少有一些简短和有效的实现这样做。

1tuwyuhd

1tuwyuhd1#

我不知道NumPy中是否有什么东西可以直接实现这一点,但你总是可以自己实现它,这不是特别聪明或高效,因为它需要多个高级索引操作,但它做了你需要的:

import numpy as np

def get_with_default(a, indices, default=0):
    # Ensure inputs are arrays
    a = np.asarray(a)
    indices = tuple(np.broadcast_arrays(*indices))
    if len(indices) <= 0 or len(indices) > a.ndim:
        raise ValueError('invalid number of indices.')
    # Make mask of indices out of bounds
    mask = np.zeros(indices[0].shape, np.bool)
    for ind, s in zip(indices, a.shape):
        mask |= (ind < 0) | (ind >= s)
    # Only do masking if necessary
    n_mask = np.count_nonzero(mask)
    # Shortcut for the case where all is masked
    if n_mask == mask.size:
        return np.full_like(a, default)
    if n_mask > 0:
        # Ensure index arrays are contiguous so masking works right
        indices = tuple(map(np.ascontiguousarray, indices))
        for ind in indices:
            # Replace masked indices with zeros
            ind[mask] = 0
    # Get values
    res = a[indices]
    if n_mask > 0:
        # Replace values of masked indices with default value
        res[mask] = default
    return res

# Test
print(get_with_default(
    np.array([[1,2,3],[4,5,6]]),
    (np.array([0, 0, 1, 1, 2, 2]), np.array([1, 2, 2, 3, 3, 5])),
    13
))
# [ 2  3  6 13 13 13]
jaql4c8m

jaql4c8m2#

我提出这个问题是因为我在寻找完全相同的东西,我提出了下面的函数,它满足了你在二维空间中的要求,它很可能推广到N维空间。

def get_with_defaults(a, xx, yy, nodata):
   # get values from a, clipping the index values to valid ranges
   res = a[np.clip(yy, 0, a.shape[0] - 1), np.clip(xx, 0, a.shape[1] - 1)]
   # compute a mask for both x and y, where all invalid index values are set to true
   myy = np.ma.masked_outside(yy, 0, a.shape[0] - 1).mask
   mxx = np.ma.masked_outside(xx, 0, a.shape[1] - 1).mask
   # replace all values in res with NODATA, where either the x or y index are invalid
   np.choose(myy + mxx, [res, nodata], out=res)
   return res

xxyy是索引数组,a(y,x)索引。
这给出:

>>> a=np.zeros((3,2),dtype=int)
>>> get_with_defaults(a, (-1, 1000, 0, 1, 2), (0, -1, 0, 1, 2), -1)
array([-1, -1,  0,  0, -1])

作为一种替代方案,以下实现实现了相同的效果,并且更加简洁:

def get_with_default(a, xx, yy, nodata):
   # get values from a, clipping the index values to valid ranges
   res = a[np.clip(yy, 0, a.shape[0] - 1), np.clip(xx, 0, a.shape[1] - 1)]
   # replace all values in res with NODATA (gets broadcasted to the result array), where
   # either the x or y index are invalid
   res[(yy < 0) | (yy >= a.shape[0]) | (xx < 0) | (xx >= a.shape[1])] = nodata
   return res
lyr7nygr

lyr7nygr3#

我也需要一个解决这个问题的方法,但我想要一个在N维空间中有效的方法,我让马库斯的方法在N维空间中有效,包括从一个比坐标指向的维度更多的数组中进行选择。

def get_with_defaults(arr, coords, nodata):
    coords, shp = np.array(coords), np.array(arr.shape)
    # Get values from arr, clipping to valid ranges
    res = arr[tuple(np.clip(c, 0, s-1) for c, s in zip(coords, shp))]
    # Set any output where one of the coords was out of range to nodata
    res[np.any(~((0 <= coords) & (coords < shp[:len(coords), None])), axis=0)] = nodata
    return res

import numpy as np

if __name__ == '__main__':
    A = np.array([[1,2,3],[4,5,6]])
    B = np.array([[[1, -9],[2, -8],[3, -7]],[[4, -6],[5, -5],[6, -4]]])
    coords1 = [[0, 0, 1, 1, 2, 2], [1, 2, 2, 3, 3, 5]]
    coords2 = [[0, 0, 1, 1, 2, 2], [1, 2, 2, 3, 3, 5], [1, 1, 1, 1, 1, 1]]

    out1 = get_with_defaults(A, coords1, 13)
    out2 = get_with_defaults(B, coords1, 13)
    out3 = get_with_defaults(B, coords2, 13)

    print(out1)
    # [2, 3, 6, 13, 13, 13]
    print(out2)
    # [[ 2 -8]
    #  [ 3 -7]
    #  [ 6 -4]
    #  [13 13]
    #  [13 13]
    #  [13 13]]
    print(out3)
    # [-8, -7, -4, 13, 13, 13]

相关问题