python - 如何在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 函数。而且对于一维数组,这将导致索引超出范围错误,这并不是很有用......
解决方案
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
。
推荐阅读
- aws-lambda - 无服务器 nuxt 应用程序中的路由不起作用
- python - 将数据从 API 写入 CSV
- python-3.x - 如何更改python子列表中元素的大小写?
- python - RuntimeWarning:exp 中遇到溢出(记录到相对)
- java - 重复提醒通知Android(java)问题
- java - Groovy + Spock 测试“Validator”类型的 void 方法,在不满足条件时抛出错误
- java - arraylist 如何在子集算法中操作和重用
- vue.js - 状态在多个应用程序之间共享。- vuex 持久化状态
- python - 使用 hvplot 从 xarray 数据集中绘制两个数据变量
- android - 过滤器正在折叠列表视图