python - 具有多个元组的 NumPy 切片

标签 python numpy array-broadcasting numpy-slicing

考虑以下因素:

import numpy as np

arr = np.arange(3 * 4 * 5).reshape((3, 4, 5))

如果我使用 slices 对 arr 进行切片,我会得到,例如:

arr[:, 0:2, :].shape
# (3, 2, 5)

如果现在我使用 slice()tuple() 的混合来切片 arr,我得到:

arr[:, (0, 1), :].shape
# (3, 2, 5)

np.all(arr[:, (0, 1), :] == arr[:, :2, :])
# True

和:

arr[:, :, (0, 1)].shape
# (3, 4, 2)

np.all(arr[:, :, (0, 1)] == arr[:, :, :2])
# True

但是,如果我这样做:

arr[:, (0, 1), (0, 1)].shape
# (3, 2)

这基本上是 arr[:, 0, 0]arr[:, 1, 1] 连接起来的。

我期望得到:

arr[:, (0, 1), (0, 1)].shape
# (3, 2, 2)

np.all(arr[:, (0, 1), (0, 1)] == arr[:, :2, :2])
# True

但事实显然并非如此。

如果我连接两个单独的切片,我将能够获得所需的结果,即:

arr[:, (0, 1), :][:, :, (0, 1)].shape
# (3, 2, 2)

np.all(arr[:, (0, 1), :][:, :, (0, 1)] == arr[:, :2, :2])
# True

是否有可能获得与 arr[:, (0, 1), :][:, :, (0, 1)] 相同的结果,但使用单个切片

现在,这个例子不是那么有趣,因为我可以用 slice() 替换 tuple() ,但如果这不是真的,那么一切都会变成更相关,例如:

arr[:, (0, 2, 3), :][:, :, (0, 2, 3, 4)]
# [[[ 0  2  3  4]
#   [10 12 13 14]
#   [15 17 18 19]]

#  [[20 22 23 24]
#   [30 32 33 34]
#   [35 37 38 39]]

#  [[40 42 43 44]
#   [50 52 53 54]
#   [55 57 58 59]]]

其中 arr[:, (0, 2, 3), (0, 2, 3, 4)] 会是更方便的语法。

编辑

@Divakar @hpaulj 和 @MadPhysicist 评论/答案指出正确广播的 Iterable 相当于多个串联切片。

然而,事实并非如此,例如:

s = np.ix_((0, 1), (0, 1, 2, 3))
arr[s[0], slice(3), s[1]]
# [[[ 0  5 10]
#   [ 1  6 11]
#   [ 2  7 12]
#   [ 3  8 13]]
# 
#  [[20 25 30]
#   [21 26 31]
#   [22 27 32]
#   [23 28 33]]]

但是:

arr[(0, 1), :, :][:, :3, :][:, :, (0, 1, 2, 3)]
# [[[ 0  1  2  3]
#   [ 5  6  7  8]
#   [10 11 12 13]]
# 
#  [[20 21 22 23]
#   [25 26 27 28]
#   [30 31 32 33]]]

和:

np.all(arr[:2, :3, :4] == arr[(0, 1), :, :][:, :3, :][:, :, (0, 1, 2, 3)])
# True

np.all(arr[s[0], slice(3), s[1]] == arr[(0, 1), :, :][:, :3, :][:, :, (0, 1, 2, 3)])
# False

最佳答案

如果您想实现以编程方式切片数组的能力,答案是 slice 对象,而不是索引序列。例如,:2 变为 slice(None, 2):

np.all(arr[:, slice(None, 2), slice(None, 2)] == arr[:, :2, :2])

切片选择轴的一部分。索引数组则不然:它们选择单个元素。索引的形状决定了这种情况下输出的形状。

如果要跨多个维度选择任意索引,索引数组的形状必须与所需输出的形状相同,或广播到它:

arr[:, [[0], [2], [3]], [[0, 2, 3, 4]]]

关于python - 具有多个元组的 NumPy 切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56328467/

相关文章:

python - 如何在 python 中使用 RSA SHA-256 签署 token ?

python - 使用通配符在列表中查找字符串

python - 圣人数学重复过程

Python NumPy : How to create the following list of lists

Python 张量积

python - 如何打印与最小残差关联的值 - Python

python - Numpy:通过分箱从关联中查找最小值和最大值

python - Julia:向量化序列的乘积

python - 指定大小的随机子集的 Numpy 列表

Python:矢量化/广播会提高速度吗?