📅  最后修改于: 2023-12-03 15:19:27.356000             🧑  作者: Mango
numpy.isinf函数是numpy库中的一个用于判断一个数是否为无穷大的函数。该函数接受一个参数,如果参数是无穷大则返回True,否则返回False。
numpy.isinf(x, *args, **kwargs)
import numpy as np
# 判断某个数是否为正无穷大或负无穷大
num1 = np.inf
num2 = -np.inf
num3 = 0
print(np.isinf(num1)) # True
print(np.isinf(num2)) # True
print(np.isinf(num3)) # False
import numpy as np
arr = np.array([1, 2, np.inf, 4, np.nan])
print(np.isinf(arr)) # [False False True False False]
import numpy as np
arr = np.array([1, 2, np.inf, 4, np.nan])
arr[np.isinf(arr)] = 0
print(arr) # [1. 2. 0. 4. nan]
import numpy as np
num1 = np.nan
print(np.isinf(num1)) # False
import numpy as np
print(np.isinf("abc")) # TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'