在 NumPy 中測試陣列值的有限性
要測試陣列值的有限性,請在 Python NumPy 中使用 **numpy.isfinite()** 方法。如果 x 不是正無窮大、負無窮大或 NaN,則返回 True;否則返回 false。如果 x 是標量,則為標量。
此條件在輸入上廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立了一個未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。
步驟
首先,匯入所需的庫 -
import numpy as np
建立一個包含一些無窮大值的陣列 -
arr = np.array([1, 2, 10, 50, -np.inf, 0., np.inf])
顯示陣列 -
print("Array...
", arr)獲取陣列的型別 -
print("
Our Array type...
", arr.dtype)
獲取陣列的維度 -
print("
Our Array Dimensions...
",arr.ndim)獲取陣列中的元素數量 -
print("
Number of elements...
", arr.size)
要測試陣列值的有限性,請在 Python NumPy 中使用 numpy.isfinite() 方法 -
print("
Test array for finiteness...
",np.isfinite(arr))示例
import numpy as np
# Create an array with some inf values
arr = np.array([1, 2, 10, 50, -np.inf, 0., np.inf])
# Display the array
print("Array...
", arr)
# Get the type of the array
print("
Our Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimensions...
",arr.ndim)
# Get the number of elements in the Array
print("
Number of elements...
", arr.size)
# To test array values for finiteness, use the numpy.isfinite() method in Python Numpy
print("
Test array for finiteness...
",np.isfinite(arr))輸出
Array... [ 1. 2. 10. 50. -inf 0. inf] Our Array type... float64 Our Array Dimensions... 1 Number of elements... 7 Test array for finiteness... [ True True True True False True False]
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP