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:
JavaScript
x
16
16
1
>>> a = np.array([[1,2],[3,4]])
2
>>> a
3
array([[1, 2],
4
[3, 4]])
5
6
>>> a[:,np.newaxis] # <-- the shape of the rows are unchanged
7
array([[[1, 2]],
8
9
[[3, 4]]])
10
>>> a[ ,np.newaxis] # <-- the shape of the rows changed from horizontal to vertical
11
array([[[1],
12
[2]],
13
14
[[3],
15
[4]]])
16
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.