+1 vote
in Programming Languages by (51.8k points)
How can I check if any value in a given array is NaN?

1 Answer

+3 votes
by (66.8k points)
selected by
 
Best answer

You can use Numpy functions isnan() and any() to check if any value in a given array is NaN.

Here is an example:

>>> import numpy as np

>>> aa=np.array([[np.nan, 0, 1],[4,6,np.nan],[8,np.nan,9]])

>>> aa

array([[nan,  0.,  1.],

       [ 4.,  6., nan],

       [ 8., nan,  9.]])

>>> np.isnan(aa)

array([[ True, False, False],

       [False, False,  True],

       [False,  True, False]])

>>> np.any(np.isnan(aa))

True

>>> 

Related questions


...