Python numpy.isnan()
Python numpy.isnan()函数测试元素是否为NaN,并将结果作为布尔数组返回。
语法 :
numpy.isnan(array [, out])
参数 :
array : [array_like]输入数组或对象的元素,我们需要测试是否为无穷大。
out : [ndarray, optional]输出数组与结果放在一起。它的类型被保留,并且它必须有合适的形状来容纳输出。
返回 :
含有结果的布尔数组。对于标量输入,结果是一个新的布尔值,如果输入是正的或负的无穷大,则值为True;否则值为False。
对于数组输入,结果是一个布尔数组,其形状与输入相同,当输入的相应元素为正或负无穷大时,其值为真。其他情况下的值是假的。
代码 1 :
# Python Program illustrating
# numpy.isnan() method
import numpy as geek
print("Is NaN : ", geek.isnan(1), "\n")
print("Is NaN : ", geek.isnan(0), "\n")
# not a number
print("Is NaN : ", geek.isnan(geek.nan), "\n")
# infinity
print("Is NaN : ", geek.isnan(geek.inf), "\n")
print("Is NaN : ", geek.isnan(geek.NINF), "\n")
x = geek.array([-geek.inf, 0., geek.inf])
y = geek.array([2, 2, 2])
print("Checking for NaN : ", geek.isnan(x, y))
输出 :
Is NaN : False
Is NaN : False
Is NaN : True
Is NaN : False
Is NaN : False
Checking for NaN : [0 0 0]
代码 2 :
# Python Program illustrating
# numpy.isnan() method
import numpy as geek
# Returns True/False value for each element
b = geek.arange(20).reshape(5, 4)
print("\n",b)
print("\nIs NaN(Not a Number): \n", geek.isnan(b))
b = [[1j],
[geek.nan]]
print("\nIs NaN(Not a Number) : \n", geek.isnan(b))
输出 :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
Is NaN(Not a Number):
[[False False False False]
[False False False False]
[False False False False]
[False False False False]
[False False False False]]
Is NaN(Not a Number) :
[[False]
[ True]]