如何在numpy数组中找到nan值的坐标? [重复]

时间:2023-01-11 07:29:34

This question already has an answer here:

这个问题在这里已有答案:

I tried np.where but without success:

我试过np.where但没有成功:

>>>a = np.array([np.nan, 1])
>>>np.where(a == np.nan)
(array([], dtype=int64),)

2 个解决方案

#1


1  

You need to change

你需要改变

np.where(a == np.nan)

to

np.where(np.isnan(a))

NaN values always return false in equality checks, even with another NaN value. So you need to use special functions to check for NaN like np.isnan.

即使使用另一个NaN值,NaN值也始终在等式检查中返回false。因此,您需要使用特殊函数来检查NaN,如np.isnan。

#2


0  

import numpy as np

x = np.array([0,0,-1,1,np.nan, 0, 1, np.nan])
print np.where(np.isnan(x))

Returns:

(array([4, 7]),)

#1


1  

You need to change

你需要改变

np.where(a == np.nan)

to

np.where(np.isnan(a))

NaN values always return false in equality checks, even with another NaN value. So you need to use special functions to check for NaN like np.isnan.

即使使用另一个NaN值,NaN值也始终在等式检查中返回false。因此,您需要使用特殊函数来检查NaN,如np.isnan。

#2


0  

import numpy as np

x = np.array([0,0,-1,1,np.nan, 0, 1, np.nan])
print np.where(np.isnan(x))

Returns:

(array([4, 7]),)