简体   繁体   English

Python Numpy; 冒号和省略号索引的区别

[英]Python Numpy; difference between colon and ellipsis indexing

I have been experimenting with Numpy array indexing using both colon and ellipsis.我一直在尝试使用冒号和省略号进行 Numpy 数组索引。 However, I cannot understand the results that I am getting.但是,我无法理解我得到的结果。

Below is the example code:下面是示例代码:

>>> 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]]])

The original is (2,2)原来是(2,2)

With:, it becomes (2,1,2).有了:,它就变成了 (2,1,2)。 The new axis added after the first dimension.在第一个维度之后添加的新轴。

With... the shape is (2,2,1), the new shape is added last.使用...形状为 (2,2,1),最后添加新形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM