Numpy indexing: shouldn't an ellipse be returned redundant?

While trying to correctly understand numpy indexing rules, I came across the following. I used to think that the final Ellipsis in the index does nothing. Trivial, right? Also, this is not the case:

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 does anyone know, tell me, is this a bug or function? And if the latter, what is the rationale?

Thanks in advance Paul

+1
source share
1 answer

Obviously, there is some ambiguity in the interpretation of the index [[1, 0]]. Perhaps the same is discussed here:

Extended slice when passing a list instead of a tuple in numpy

I will try another array to see if this is clear.

In [312]: D2=np.array([[0,0],[1,1],[2,2]])
In [313]: D2
Out[313]: 
array([[0, 0],
       [1, 1],
       [2, 2]])

In [316]: D2[[[1,0,0]]]
Out[316]: 
array([[1, 1],
       [0, 0],
       [0, 0]])
In [317]: _.shape
Out[317]: (3, 2)

: ... , (1,3)

In [318]: D2[[[1,0,0]],:]
Out[318]: 
array([[[1, 1],
        [0, 0],
        [0, 0]]])
In [319]: _.shape
Out[319]: (1, 3, 2)
In [320]: D2[np.array([[1,0,0]])]
Out[320]: 
array([[[1, 1],
        [0, 0],
        [0, 0]]])
In [321]: _.shape
Out[321]: (1, 3, 2)

, , (3,1,2)

In [323]: D2[np.array([[1,0,0]]).T,:]
...
In [324]: _.shape
Out[324]: (3, 1, 2)

: ..., , [], 1- :

In [330]: D2[[1,0,0]].shape
Out[330]: (3, 2)
In [331]: D2[[[1,0,0]]].shape
Out[331]: (3, 2)
In [333]: D2[[[[1,0,0]]]].shape
Out[333]: (1, 3, 2)
In [334]: D2[[[[[1,0,0]]]]].shape
Out[334]: (1, 1, 3, 2)
In [335]: D2[np.array([[[[1,0,0]]]])].shape
Out[335]: (1, 1, 1, 3, 2)

, . , "": D2[(1,2)] D2[1,2]. numpy (numeric) [] .

:

, - .

a ... - D2[[[0,1]]] D2[([0,1],)].

@eric/s seburg

- ( length <= np.MAXDIMS, , None ).

[[1,2]] - 1 , , .. ([1,2],). [[1,2]],... .

+5

Source: https://habr.com/ru/post/1664318/


All Articles