N-D индексирование по умолчанию в NumPy

#python #arrays #numpy #fallback

#python #массивы #numpy #запасной вариант

Вопрос:

Могу ли я индексировать 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]
  

Я ищу для этого какую-нибудь встроенную функцию. Если такого не существует, то для этого есть хотя бы какая-то короткая и эффективная реализация.

Комментарии:

1. np.take имеет какое-то тестирование границ.

Ответ №1:

Я пришел к этому вопросу, потому что искал точно то же самое. Я придумал следующую функцию, которая выполняет то, что вы просите для 2-х измерений. Вероятно, его можно было бы обобщить на 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
  

xx и yy являются индексным массивом, 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
  

Комментарии:

1. Спасибо! Голосование, похоже, хороший вариант.

Ответ №2:

Я не знаю, есть ли что-нибудь в 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]
  

Комментарии:

1. Будет принимать его до тех пор, пока кто-нибудь не найдет какую-нибудь готовую встроенную операцию NumPy для решения задачи.

Ответ №3:

Мне также нужно было решение для этого, но я хотел решение, которое работало в 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) amp; (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]