Skip to content
Advertisement

Python Numpy; difference between colon and ellipsis indexing

I have been experimenting with Numpy array indexing using both colon and ellipsis. 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]]])

Advertisement

Answer

The original is (2,2)

With :, it becomes (2,1,2). The new axis added after the first dimension.

With … the shape is (2,2,1), the new shape is added last.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement