Open In App

Return the maximum of an array along axis 0 or maximum ignoring any NaNs in Python

Last Updated : 15 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to return the maximum of an array along axis 0 or maximum ignoring any NaNs in Python.

Example

Input: [[ 1.  0.  3.]
        [10. nan 30.]
        [nan 10. 20.]]
Output: [10. 10. 30.]
Explanation: An array with maximum values.

nanmax method in Python

Python provides a nanmax method that returns the maximum of an array along a specified axis by ignoring any NaN values. nanmax method is present in the NumPy package which returns the value of an array or an array along any specified axis by ignoring the NaN values. Let’s look into the syntax of nanmax method and discuss the parameters that are accepted by this method.

Syntax: numpy.nanmax(arr, axis=None, out=None, keepdims = no_value)

Parameters:

  • arr:- Input array
  • axis:- axis=0 represents along the column, axis=1 represents along the row.
  • out:- Different array where we want to store the output. It’s dimensions should match with dimensions of expected output.
  • keepdims:- If keepdims value is set to true then the axes which are reduced are left in the result with dimension 1.

Returns a scalar value (if axis is none) or an array with maximum values along specified axis.

Example 1:

The resultant array consists of values that are maximum from each column, as we specified axis=0 in nanmax method.

Python3




# import required packages
import numpy as np
 
# creating a numpy array with few Nan values
arr = np.array([[1, 0, 3], [10, np.nan, 30],
                [np.nan, 10, 20]])
 
# display the input array
print("Input array\n", arr)
 
# return the maximum values of an array
# along specified axis by ignoring NaNs
print("Max Array-", np.nanmax(arr, axis=0))


Output:

Input array
 [[ 1.  0.  3.]
 [10. nan 30.]
 [nan 10. 20.]]
Max Array- [10. 10. 30.]

Example 2:

The resultant array consists of values that are maximum from each row as we specified axis=1 in nanmax method.

Python3




# import required packages
import numpy as np
 
# creating a numpy array with few Nan values
arr = np.array([[1, 0, 3], [10, np.nan, 30],
                [np.nan, 10, 20]])
 
# display the input array
print("Input array\n", arr)
 
# return the maximum values of an array
# along specified axis by ignoring NaNs
print("Max Array-", np.nanmax(arr, axis=1))


Output:

Input array
 [[ 1.  0.  3.]
 [10. nan 30.]
 [nan 10. 20.]]
Max Array- [ 3. 30. 20.]

Example 3:

In this example, we didn’t specify the axis parameter in nanmax method. So it considers the axis=None value and returns the maximum value in the given array without considering NaN values.

Python3




# import required packages
import numpy as np
 
# creating a numpy array with few Nan values
arr = np.array([[1, 0, 3], [10, np.nan, 30],
                [np.nan, 10, 20]])
 
# display the input array
print("Input array\n", arr)
 
# return the maximum value in an array
# by ignoring NaNs
print("Max value in array-", np.nanmax(arr))


Output:

Input array
 [[ 1.  0.  3.]
 [10. nan 30.]
 [nan 10. 20.]]
Max value in array- 30.0


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads