# Python: Ellipsis (...) / Numpy: axis=-1 / np.newaxis

Yao Yao on June 12, 2019

## Ellipsis

Ellipsis is a python constant, just like False, True and None.

... is the ellipsis literal; it’s equivalent to Ellipsis.

In [9]: Ellipsis
Out[9]: Ellipsis

In [10]: ...
Out[10]: Ellipsis


When used for slicing, ... usually (depending on the __getitem__ implementation) means “all the other dimensions (between the specified sentinels)” (: means “all the elements in that dimension”).

It’s useful when you are handling variant-dimension arrays/matrices. Say you need a function to return “the last element(s) of each dimension”. (See Stack Overflow: “Slicing” in Python Expressions documentation)

• If it’s a 1-D, you need array[-1]
• If it’s a 2-D, you need array[:, -1]
• If it’s a 3-D, you need array[:, :, -1]
• ……
• No matter how many dimensions, you can always have array[..., -1]

## axis=-1

Just like list[-1] indicates the last elements, axis=-1 means “the last dimension” (See Stack Overflow: What is the meaning of axis=-1 in keras.argmax?):

• It’s the 2nd dimension of a 2-D array
• It’s the 3rd dimension of a 3-D array

## np.newaxis

Used to increase the dimension of the current array, directly from the slicing processes.

A great illustration of how it works is from Stack Overflow: How does numpy.newaxis work and when to use it?:

Tensorflow has a similar tf.newaxis; it works the same way:

In [2]:  t = tf.constant([[1., 2., 3.], [4., 5., 6.]])

In [6]: t[:, 1].shape
Out[6]: TensorShape([2])

In [7]: t[:, 1, tf.newaxis].shape
Out[7]: TensorShape([2, 1])

In [8]: t[tf.newaxis, :, 1].shape
Out[8]: TensorShape([1, 2])

• t[:, 1] is the vector [2, 5]
• t[:, 1, tf.newaxis] makes a $2 \times 1$ matrix: 2 rows, each being a one-element vector
• t[tf.newaxis, :, 1] makes a $1 \times 2$ matrix: 1 row, containing a two-element vector