python中的NaN和有效性检查

Leo*_*fox 8 python numpy nan

我正在做一个我优化一些值的程序.由于方程式,我的值有时是NaN

我的问题,一些条目是NaN.

我想知道是否有测试来检查它们的逻辑有效性,以便我可以跳过这些值并重试.

到目前为止,我已经尝试过检查

a==np.nan, a==nan, b=a a==b
Run Code Online (Sandbox Code Playgroud)

无济于事.

我希望你能帮帮我

谢谢

riz*_*iza 26

使用numpy,

import numpy as np
np.isnan(np.nan) # returns True
Run Code Online (Sandbox Code Playgroud)


n00*_*dle 7

从Python 2.6开始,您想要import math并使用它math.isnan(a).

http://docs.python.org/library/math.html#math.isnan