I think I understand the indexing of array in python/numpy correctly. But today I met a problem as follows:
I have a 6-d array e.g. A
and A.shape = (11,1,9,1,5,7)
.
Then I use the indexing as follows:
B = A[:,0,0,0,[3,4,2],0]
and B.shape = (11,3)
as expected;
C = A[:,0,0,0,[3,4,2],:]
and C.shape = (11,3,7)
as expected;
But when I say:
D = A[:,0,:,0,[3,4,2],0]
and D.shape
should be (11,9,3)
as I can expect, however, python returned the D.shape = (3, 11, 9)
.
And I'm really confused about the shape of array D
.
Is there any one can give me a brief explanation? Thanks a lot!
1 Answer 1
As discussed in https://docs.scipy.org/doc/numpy-1.12.0/reference/arrays.indexing.html#combining-advanced-and-basic-indexing
A[:,0,:,0,[3,4,2],0]
indexes with the 'advanced' list, [3,4,2]
producing the size 3 dimension. And the 1st and 3rd dimensions are added on after, resulting in the (3,11,9) shape.
This behavior is somewhat controversial, especially when the other indices are scalars. The justification given in the docs is clearer when there are two indexing lists.
basic&advanced
indexing docs about mixing slices and lists. There's some ambiguity, andnumpy
opts to put the slice dimensions last. This behavior has also been discussed in previous SO questions.