为什么用括号和逗号索引 numpy 数组的行为不同?

Why does indexing numpy arrays with brackets and commas differ in behavior?

我倾向于用方括号索引 numpy 数组(矩阵),但我注意到当我想对数组(矩阵)进行切片时,我必须使用逗号表示法。为什么是这样?例如,

>>> x = numpy.array([[1, 2], [3, 4], [5, 6]])
>>> x
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> x[1][1]
4                 # expected behavior
>>> x[1,1]
4                 # expected behavior
>>> x[:][1]
array([3, 4])     # huh?
>>> x[:,1]
array([2, 4, 6])  # expected behavior

这个:

x[:, 1]

表示"take all indices of x along the first axis, but only index 1 along the second".

这个:

x[:][1]

表示"take all indices of x along the first axis (so all of x), then take index 1 along the first axis of the result"。您将 1 应用于错误的轴。

x[1][2]x[1, 2] 只是等价的,因为用整数索引数组会将所有剩余的轴移向形状的前面,所以 x[1] 的第一个轴是第二个x 的轴。这根本不能一概而论;您应该几乎总是使用逗号而不是多个索引步骤。

对数组进行多维切片时,如果提供的索引少于轴数,则缺失的索引被视为完整切片。 因此,当您调用 x[:][1] 时本质上是 x[:,:][1,:] 因此,x[:,:] 只会 return x 本身。

真正的解释是在 2 个括号 [][] 的情况下,第一个括号 (x[] ) 创建一个引用多维数组的第一个维度的临时数组。

而 x[][] 的第二个括号将应用于已创建的临时数组。

所以没有真正的危险发生。由于所描述的行为,当您将切片与多个括号结合使用时,基本上第一个括号已经选择了 numpy 数组的一部分,而第二个括号进入了已经选择的数组。

这里也有解释:numpy.org/devdocs/user/basics.indexing.html