In this article we will discuss how to get the maximum / largest value in a Numpy array and its indices using numpy.amax().

## numpy.amax()

Python’s numpy module provides a function to get the maximum value from a Numpy array i.e.

Arguments :

• a : numpy array from which it needs to find the maximum value.
• axis : It’s optional and if not provided then it will flattened the passed numpy array and returns the max value in it.
• If it’s provided then it will return for array of max values along the axis i.e.
• If axis=0 then it returns an array containing max value for each columns.
• If axis=1 then it returns an array containing max value for each row.

Let’s look in detail,

## Find maximum value & its index in a 1D Numpy Array:

Let’s create a 1D numpy array from a list i.e.

Find maximum value:

Now let’s use numpy.amax() to find the maximum value from this numpy array by passing just array as argument i.e.

Output:

It returns the maximum value from the passed numpy array i.e. 17

Find index of maximum value :

Get the array of indices of maximum value in numpy array using numpy.where() i.e.

Output:

In numpy.where() when we pass the condition expression only then it returns a tuple of arrays (one for each axis) containing the indices of element that satisfies the given condition. As our numpy array has one axis only therefore returned tuple contained one array of indices.

## Find maximum value & its index in a 2D Numpy Array

Let’s create a 2D numpy array i.e.

Contents of the 2D numpy array are,

Find max value in complete 2D numpy array

To find maximum value from complete 2D numpy array we will not pass axis in numpy.amax() i.e.

It will return the maximum value from complete 2D numpy arrays i.e. in all rows and columns.

Find max values along the axis in 2D numpy array | max in rows or columns:

If we pass axis=0 in numpy.amax() then it returns an array containing max value for each column i.e.

Output:

If we pass axis = 1 in numpy.amax() then it returns an array containing max value for each row i.e.

Output:

### Find index of maximum value from 2D numpy array:

Contents of the 2D numpy array arr2D are,

Let’s get the array of indices of maximum value in 2D numpy array i.e.

Output:

### numpy.amax() & NaN

numpy.amax() propagates the NaN values i.e. if there is a NaN in the given numpy array then numpy.amax() will return NaN as maximum value. For example,

Output:

If you want to ignore the NaNs while finding the max values from numpy then use numpy.nanmax() instead.

Complete example is as follows,

Output

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.