One of the most beneficial but straightforward indexing routines in NumPy is the npindex(). This routine provides us iterator that returns the indices of elements in an N-dimensional array.
This short article will discuss the ndindex() routine and its use in NumPy.
The syntax of the ndindex routine is as shown:
It accepts the shape of the array as a scalar integer or tuple of integers.
Consider the example shown below:
for index in np.ndindex(2,3):
In this case, we use the ndindex function to get the index of the elements in an array of shapes (2,3).
The above code should return:
We can also pass the shape as a single tuple. For example:
arr = np.array([[1,2,3], [4,5,6]])
for index in np.ndindex((arr.shape)):
Here, we use the arr.shape property as the value of the ndindex() function.
In this one, we covered the ndindex() function in NumPy and how to use it. Feel free to explore the docs to learn more.