📜  Python中的 numpy.isfinite()

📅  最后修改于: 2022-05-13 01:54:25.152000             🧑  作者: Mango

Python中的 numpy.isfinite()

numpy.isfinite()函数按元素测试它是否是有限的(不是无穷大或不是不是数字),并将结果作为布尔数组返回。句法 :

numpy.isfinite(array [, out])

参数 :

array : [array_like]Input array or object whose elements, 
        we need to test for infinity
out   : [ndarray, optional]Output array placed with result.
       Its type is preserved and it must be of the right 
       shape to hold the output.

返回 :

boolean array containing the result

代码 1:

Python
# Python Program illustrating
# numpy.isfinite() method
  
import numpy as geek
 
print("Finite : ", geek.isfinite(1), "\n")
 
print("Finite : ", geek.isfinite(0), "\n")
 
# not a number
print("Finite : ", geek.isfinite(geek.nan), "\n")
 
#  infinity
print("Finite : ", geek.isfinite(geek.inf), "\n")
 
print("Finite : ", geek.isfinite(geek.NINF), "\n")


Python
# Python Program illustrating
# numpy.isfinite() method
   
import numpy as geek
  
# Returns True/False value for each element
b = geek.arange(20).reshape(5, 4)
                
print("\n",b)
print("\nIs Finite : \n", geek.isfinite(b))
 
 
b = [[1j],
     [geek.inf]]
print("\nIs Finite : \n", geek.isfinite(b))


输出 :

Finite :  True 

Finite :  True 

Finite :  False 

Finite :  False 

Finite :  False 

代码 2:

Python

# Python Program illustrating
# numpy.isfinite() method
   
import numpy as geek
  
# Returns True/False value for each element
b = geek.arange(20).reshape(5, 4)
                
print("\n",b)
print("\nIs Finite : \n", geek.isfinite(b))
 
 
b = [[1j],
     [geek.inf]]
print("\nIs Finite : \n", geek.isfinite(b))

输出 :

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]

Is Finite : 
 [[ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]
 [ True  True  True  True]]

Is Finite : 
 [[ True]
 [False]]

参考: https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.isfinite.html