Reputation: 1403
Why this indexing result in different arrays?
import numpy as np
x = np.array(range(0,2*3*4)).reshape(2,3,4)
print(x[0,:,[2,3]])
print(x[0,:,2:])
the first output is
[[ 2 6 10]
[ 3 7 11]]
the second one is
[[ 2 3]
[ 6 7]
[10 11]]
in the second case 2:
means take from the 2nd value until the end, the last column of that dim is the 3 column, that means that it is taking the 2nd and 3rd dimensions, therefore that is the same as [2,3], so what is the difference between both ways of indexing arrays?
Upvotes: 0
Views: 548
Reputation: 1458
In the first case, x[0,:,[2,3]]
implies that numpy
will return an array such that x[0,:,2]
is the first item followed by x[0,:,3]
. In the second case, x[0,:,2:]
you are asking numpy
for 2nd and 3rd column of the 0th matrix.
The indexing documentation is available here.
Upvotes: 1
Reputation: 13103
The rules are different for indexing with an array (or a list) of integers and for slicing. This is explained in-depth in the documentation, in particular in the part on mixing advanced and basic indexing.
Upvotes: 2