Saturday, December 28, 2024
Google search engine
HomeLanguagesReturn the maximum of an array or maximum ignoring any NaNs in...

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

In this article, we will cover how to return the maximum of an array or maximum by ignoring any NaNs in Python using NumPy.

Example

Input: [  -1.   -2.   nan 1000.]
Output: 1000.0
Explanation: maximum value of the array ignoring nans.

One approach to use the built-in Python function max(), along with the filter() function and the math.isnan() function from the math module to ignore any NaN values in the array.

Here is an example of how this can be done

Python3




import math
 
def ignore_nan(arr):
    return max(filter(lambda x: not math.isnan(x), arr))
 
# Example usage
arr = [-1, -2, float('nan'), 1000]
result = ignore_nan(arr)
print(result)  # Output: 1000


Output

1000

This approach first filters out any NaN values from the array using the filter() function, which applies the function provided as the first argument (in this case, math.isnan()) to each element in the array and returns a new iterator with only the elements for which the function returns True. The max() function is then applied to this filtered iterator to find the maximum value.

NumPy.nanmax() method

The numpy.nanmax() method from NumPy returns the highest value or the maximum value in an array or the highest value along an axis, ignoring any NaNs.

Syntax: numpy.nanmax(a, axis=None, out=None)

Parameters:

  • a: array like object.
  • axis: by default None.
  • out : by default None.

Return: maximum array value(a scalar value if axis is none)or array with maximum value along specified axis.

Example 1:

In this example, the NumPy package is imported. An array is created using numpy.array() method which contains nan and other values and np.nanmax() returns the maximum value of the array ignoring nans. The shape, datatype, and dimensions of the array can be found by .shape, .dtype, and .ndim attributes.

Python3




import numpy as np
 
# Creating an array
array = np.array([-1, -2 , np.nan, 1000])
print(array)
 
# shape of the array is
print("Shape of the array is : ",array.shape)
 
# dimension of the array
print("The dimension of the array is : ",array.ndim)
 
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
 
# computing the maximum or array ignoring Nans
print(np.nanmax(array))


Output:

[  -1.   -2.   nan 1000.]
Shape of the array is :  (4,)
The dimension of the array is :  1
Datatype of our Array is :  float64
1000.0

Example 2:

In case our array contains np.inf or positive infinity np.nanmax() method returns inf.

Python3




import numpy as np
 
# Creating an array
array = np.array([-1, -2 , np.inf,np.nan, 1000])
print(array)
 
# shape of the array is
print("Shape of the array is : ",array.shape)
 
# dimension of the array
print("The dimension of the array is : ",array.ndim)
 
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
 
# computing the maximum or array ignoring Nans
print(np.nanmax(array))


Output:

[  -1.   -2.   inf   nan 1000.]
Shape of the array is :  (5,)
The dimension of the array is :  1
Datatype of our Array is :  float64
inf

Example 3:

If all the elements in the array are nan, then the method raises a runtime warning saying “RuntimeWarning: All-NaN slice encountered”.

Python3




import numpy as np
 
# Creating an array
array = np.array([np.nan, np.nan])
print(array)
 
# shape of the array is
print("Shape of the array is : ",array.shape)
 
# dimension of the array
print("The dimension of the array is : ",array.ndim)
 
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
 
# computing the maximum or array ignoring Nans
print(np.nanmax(array))


Output:

[nan nan]
Shape of the array is :  (2,)
The dimension of the array is :  1
Datatype of our Array is :  float64
nan
RuntimeWarning: All-NaN slice encountered

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments