**The Numpy nanargmin() method is used to get the indices of the minimum element from an array (single-dimensional array) or any row or column (multidimensional array) of any given array. But if there are any NaNs in the array, it ignores those values and returns indices.**

**np.nanargmin()**

The np.nanargmin() function returns indices of the min element of the array in a particular axis, ignoring NaNs. The results cannot be trusted if a slice contains only NaNs and Infs.

**Syntax**

numpy.nanargmin(arr, axis=None)

**Parameters**

The NumPy nanargmin() function takes two arguments as a parameter:

- arr: The array from which we want the indices of the min element.
- axis: By default, it is None. But for the multidimensional array, if we’re going to find an index of any maximum of element row-wise or column-wise, we have to give axis=1 or axis=0, respectively.

**Return Value**

NumPy nanargmin() function returns an array of the same shape of the given array containing the indices of the minimum elements.

**Finding the index of a minimum element from a 1D array**

See the following code.

#Importing numpy import numpy as np #We will create a 2D array #Of shape 4x3 arr = np.array([(14, 29, 34), (41, 55, 46), (1, 38, 29), (5, 57, 52)]) #Printing the array print("The array is: ") print(arr) print("Shape of the array is: ", np.shape(arr)) #Now we will find the maximum value for some cases #Maximum value of the whole array print("Maximum value of the whole array is: ", np.amax(arr)) #Maximum value of each row a = np.amax(arr, axis=1) print("Maximum value of each row of the array is: ", a) #Maximum value of each column b = np.amax(arr, axis=0) print("Maximum value of each column of the array is: ", b)

**Output**

The array is: [ 40. 24. nan 63. 121. 4. 64.] Shape of the array is : (7,) Index of minimum value of the given array is: 5

**Explanation**

In this program, we have first declared an array with some random numbers, including a nan given by the user.

Then we have printed the shape (size) of the array.

Then we have called nanargmin() to get the index of the minimum element from the array. We can see that the minimum element of this array is 121 (except nan), which is at position 4, so the output is 4.

**Finding indices of maximum elements from Multidimensional Array**

See the following code.

#Importing numpy import numpy as np #We will create a 2D array #Of shape 4x3 arr = np.array([(14, 9, 34), (np.nan, 55, 4), (1, 3, np.nan), (5, np.nan, 41)]) #Printing the array print("The array is: ") print(arr) print("Shape of the array is: ", np.shape(arr)) #Now we will find the indices of minimum value for some cases #Indices of minimum value of each row a = np.nanargmin(arr, axis=1) print("Indices of minimum value of each row of the array is: ", a) #Indices of minimum value of each column b = np.nanargmin(arr, axis=0) print("Indices of minimum value of each column of the array is: ", b)

**Output**

[[14. 9. 34.] [nan 55. 4.] [ 1. 3. nan] [ 5. nan 41.]] Shape of the array is: (4, 3) Indices of minimum value of each row of the array is: [1 2 0 0] Indices of minimum value of each column of the array is: [2 2 1]

**Explanation**

In the above program, we have first declared the matrix of size 4×3, and you can see the shape of the matrix also, which is (4,3).

Then we have called nanargmin() to get the output of different cases.

In the first case, we have passed *arr and axis=1,* which returns an array of size 4 containing indices of all the minimum elements from each row. In the second case, we have passed *arr and axis=0*, which returns an array of size 3 containing indices of all the minimum elements from each column.