This question already has an answer here:
这个问题在这里已有答案:
- How to get the indices list of all NaN value in numpy array? 2 answers
如何获取numpy数组中所有NaN值的索引列表? 2个答案
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]),)