While trying to properly understand numpy indexing rules I stumbled across the following. I used to think that a trailing Ellipsis in an index does nothing. Trivial isn't it? Except, it's not actually true:
Python 3.5.2 (default, Nov 11 2016, 04:18:53)
[GCC 4.8.5] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> D2 = np.arange(4).reshape((2, 2))
>>>
>>> D2[[1, 0]].shape; D2[[1, 0], ...].shape
(2, 2)
(2, 2)
>>> D2[:, [1, 0]].shape; D2[:, [1, 0], ...].shape
(2, 2)
(2, 2)
>>> # so far so expected; now
...
>>> D2[[[1, 0]]].shape; D2[[[1, 0]], ...].shape
(2, 2)
(1, 2, 2)
>>> # ouch!
...
>>> D2[:, [[1, 0]]].shape; D2[:, [[1, 0]], ...].shape
(2, 1, 2)
(2, 1, 2)
Now could someone in the know advise me as to whether this is a bug or a feature? And if the latter, what's the rationale?
Thanks in advance, Paul
Evidently there's some ambiguity in the interpretation of the
[[1, 0]]
index. Possibly the same thing discussed here:Advanced slicing when passed list instead of tuple in numpy
I'll try a different array, to see if it makes things any clear
Use of
:
or...
or making the index list an array, all treat it as a (1,3) index, and expand the dimensions of the result accordinglyNote that if I apply transpose to the indexing array I get a (3,1,2) result
Without
:
or...
, it appears to strip off one layer of[]
before applying it to the 1st axis:I think there's a backward compatibility issue here. We know that the tuple layer is 'redundant':
D2[(1,2)]
is the same asD2[1,2]
. But for compatibility for early versions ofnumpy
(numeric
) that first[]
layer may be treated in the same way.In that November question, I noted:
The addition of a
...
is another way of separating theD2[[[0,1]]]
fromD2[([0,1],)]
.From
@eric/s
pull requestseburg
explains[[1,2]]
is a 1 element list with a list, so it is considered a tuple, i.e.([1,2],)
.[[1,2]],...
is a tuple already.