I came across a code where the author have used the ellipsis operator (e.g., [..., 1]) with numpy array instead of a slice operator (e.g., [:, 1]) to get arrays part.
My research on the subject:
From scipy github wiki page I've learned that both operators perform somewhat similar operations, i.e. return a slice of a multidimensional array.
I've went over this question, which have dealt with several slicing techniques of
numpyarrays, but did not find the elaboration regarding the situation when should you usesliceoperator and when it is necessary to use theellipsis, or if their functionality is identical.From
Example 1I can't spot any difference between the two operators:
Example 1:
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
A[..., 0], A[:, 0] # Out: (array([1, 4, 7]), array([1, 4, 7]))
A[..., 0] == A[:, 0] # Out: array([ True, True, True])
So my question is:
- What is the difference in using the
slicevsellipsisoperator with thenumpy.ndarrays? - Can they be used interchangeably?
- Is there any advantages in using one or the other?
I'd really appreciate an elaboration regarding my question, and thanks in advance for your time.