# np.nanargmax: Numpy nanargmax() Function

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

## Numpy nanargmax()

The nanagrmax() is a built-in Numpy function that is used to get the indices of the maximum 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.

### Syntax

```numpy.nanargmax(arr,axis=None)
```

### Parameters

The nanargmax() function takes two arguments as a parameter-

1. arr: The array from which we want the indices of the max element.
2. 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

The nanargmax() function returns an array of the same shape of the given array containing the indices of the maximum elements.

### Finding the index of a maximum element from a 1D array in Python NumPy

See the following code.

```# app.py

#Importing numpy
import numpy as np

#We will create an 1D array
arr = np.array([40, 24, np.nan, 63, 121, 4, 64])
#Printing the array
print("The array is: ", arr)
#Shape of the array
print("Shape of the array is : ", np.shape(arr))

#Now we will print index of max value of this array
print("Index of value of the given array is: ", np.nanargmax(arr))```

#### Output

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

#### 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 nanargmax() to get the index of a maximum element from the array.

We can see that the maximum 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, 29, 34), (np.nan, 55, 46), (1, 38, np.nan),
(5, np.nan, 52)])
#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 maximum value for some cases

#Indices of maximum value of each row
a = np.nanargmax(arr, axis=1)
print("Index of maximum value of each row of the array is: ", a)

#Indices of ,aximum value of each column
b = np.nanargmax(arr, axis=0)
print("Index of maximum value of each column of the array is: ", b)
```

#### Output

```The array is:
[[14. 29. 34.]
[nan 55. 46.]
[ 1. 38. nan]
[ 5. nan 52.]]
Shape of the array is:  (4, 3)
Index of maximum value of each row of the array is:  [2 1 1 2]
Index of maximum value of each column of the array is:  [0 1 3]
```

#### Explanation

In this 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 nanargmax() 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 maximum 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 maximum elements from each column.