Skip to content Skip to sidebar Skip to footer

Use Of Ellipsis In Modifying Numpy Arrays

I saw the following code here, which tries to iterate a numpy array arr and modify its elements. However, I do not quite understand what the purpose of using ellipsis (...) is here

Solution 1:

Ellipsis ... is a built-in Python symbol that is used to specify slices in N-dimensional Numpy arrays, such as a[0,...,0] is equivalent to a[0,:,:,:,0] for a 5-D array a.

The nditer objects use this syntax to make iterators writable.

Using an iterator object as an lvalue modifies the object itself instead of the location in an array it is referring to. So x[...] is the syntax that is used for dereferencing an iterator.

You could also use this syntax to access the value for reading, but this is redundant.

Also note that your syntax for op_flags is incorrect. It should be a list or a tuple.

forxin np.nditer(arr,op_flags=['readwrite']):
    x[...] = x*2;

Post a Comment for "Use Of Ellipsis In Modifying Numpy Arrays"