首页 > 解决方案 > 如何在numpy中循环回到数组的开头以获取越界索引?

问题描述

我有一个 2D numpy 数组,我想从中提取一个子矩阵。我通过如下切片数组来获得子矩阵。在这里,我想要一个 3*3 子矩阵围绕索引为 (2,3) 的项目。

>>> import numpy as np
>>> a = np.array([[0, 1, 2, 3],
...        [4, 5, 6, 7],
...        [8, 9, 0, 1],
...        [2, 3, 4, 5]])
>>> a[1:4, 2:5]
array([[6, 7],
   [0, 1],
   [4, 5]])

但我想要的是对于超出范围的索引,它会回到数组的开头并从那里继续。这是我想要的结果:

array([[6, 7, 4],
   [0, 1, 8],
   [4, 5, 2]])

我知道我可以做一些事情,比如将索引的 mod 获取到数组的宽度;但我正在寻找一个可以做到这一点的 numpy 函数。而且对于一维数组,这将导致索引超出范围错误,这并不是很有用......

标签: pythonlistnumpyindexing

解决方案


np.take确实有一个mode参数可以环绕超出范围的索引。np.take但是用于多维数组有点麻烦,因为它axis必须是一个标量。

但是,在您的特定情况下,您可以这样做:

a = np.array([[0, 1, 2, 3],
              [4, 5, 6, 7],
              [8, 9, 0, 1],
              [2, 3, 4, 5]])

np.take(a, np.r_[2:5], axis=1, mode='wrap')[1:4]

输出:

array([[6, 7, 4],
       [0, 1, 8],
       [4, 5, 2]])

编辑

此功能可能是您正在寻找的(?)

def select3x3(a, idx):
    x,y = idx
    return np.take(np.take(a, np.r_[x-1:x+2], axis=0, mode='wrap'), np.r_[y-1:y+2], axis=1, mode='wrap')

但回想起来,我建议对这种操作使用模数和花式索引(这基本上mode='wrap'是内部正在做的事情):

def select3x3(a, idx):
    x,y = idx
    return a[np.r_[x-1:x+2][:,None] % a.shape[0], np.r_[y-1:y+2][None,:] % a.shape[1]]

上述解决方案也适用于 上的任何 2d 形状a


推荐阅读