Understand NumPy Array Three Dots(…) or Ellipsis with Examples – NumPy Tutorial

By | January 11, 2024

We may find three dots (…) or ellipsis when get numpy ndarray elements. For example:

shift_logits = logits[..., :-1, :]
shift_labels = labels[..., 1:]

What this ellipsis means? In this tutorial, we will use an example to show you.

For example:

import numpy as np

x = np.arange(0,32).reshape(2, 4, 4)
print("x =")
print(x)

print("x[:,:, 1] = ")
print(x[:,:, 1])

print("x[..., 1] = ")
print(x[..., 1])

Run this code, we will see this result.

x =
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [12 13 14 15]]

 [[16 17 18 19]
  [20 21 22 23]
  [24 25 26 27]
  [28 29 30 31]]]
x[:,:, 1] = 
[[ 1  5  9 13]
 [17 21 25 29]]
x[..., 1] = 
[[ 1  5  9 13]
 [17 21 25 29]]

From the result above, we can find:

x[:,:, 1] == x[…, 1]

Actually, ellipsis mean :,:,…,: from the beginning and :,:,…,: from the ending.

For example:

if the shape of x is [3,4,5,6,]

x[:,:,2:3,:] = […,2:3,…]