Python numpy;冒号和省略号索引之间的区别

cko*_*g80 3 python indexing numpy ellipsis

我一直在尝试使用冒号和省略号进行 Numpy 数组索引。但是,我无法理解我得到的结果。

下面是示例代码:

>>> a = np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
       [3, 4]])

>>> a[:,np.newaxis]     #  <-- the shape of the rows are unchanged
array([[[1, 2]],

       [[3, 4]]])
>>> a[...,np.newaxis]   #  <-- the shape of the rows changed from horizontal to vertical
array([[[1],
        [2]],

       [[3],
        [4]]])
Run Code Online (Sandbox Code Playgroud)

hpa*_*ulj 5

原来是(2,2)

加上:,就变成(2,1,2)。在第一个维度之后添加的新轴。

对于...形状为(2,2,1),新形状最后添加。