Search code examples
pythonnumpyellipsis

For 1-D `X`, what is the difference between `X[n]` and `X[..., n]`?


When using an ellipsis (...) to index an 1-D ndarray, I would expect the expressions X[0] and X[..., 0] to be semantically identical. However, their string representations differ:

In [522]: X = arange(5) 

In [523]: repr(X[0])                         
Out[523]: '0'

In [524]: repr(X[..., 0])
Out[524]: 'array(0)'

I can't find any other differences, and indeed, they are equal according to array_equal:

In [526]: array_equal(X[0], X[..., 0])
Out[526]: True

If they are equal according to array_equal, why do they have different string representations?


Solution

  • X[0] returns a numpy.int64 object.
    When you say X[0], you are telling python to give you the object at index 0 in array X.

    X[...,0] returns a 0 dimensional numpy array.
    When you say X[...,0], you are telling python to give you the items at index 0 along the last axis of array X.