Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find max value of a list with numpy nan [duplicate]

import numpy as np

print max([np.nan, 1, 2, 3, 4])
print max([1, 2, 3, 4, np.nan])
print max([1, 2, 3, np.nan, 4])

the first will print nan as list's max value

the second will print 4 as list's max value

the third will print 4 as list's max value

Is there a solution for this problem? Make all math function just ignore nan?

like image 686
hihell Avatar asked Mar 27 '17 09:03

hihell


People also ask

How do you find the maximum value in numpy?

nanmax() to find the maximum values while ignoring nan values, as well as np. argmax() or . argmax() to find the indices of the maximum values. You won't be surprised to learn that NumPy has an equivalent set of minimum functions: np.

What is Nanmax in numpy?

numpy. nanmax() function is used to returns maximum value of an array or along any specific mentioned axis of the array, ignoring any Nan value. Syntax : numpy.nanmax(arr, axis=None, out=None, keepdims = no value)

How do you find a maximum value in a list?

Method 5: Use the max() and def functions to find the largest element in a given list. The max() function prints the largest element in the list.

How do you find the max and min value of a numpy array?

numpy. amax() will find the max value in an array, and numpy. amin() does the same for the min value.


1 Answers

Use np.nanmax() to ignore any NaNs:

In [57]: np.nanmax([np.nan, 1, 2, 3, 4])
Out[57]: 4.0
like image 123
Mazdak Avatar answered Oct 16 '22 23:10

Mazdak