在 NumPy 中計算浮點數的絕對值
要返回浮點數的絕對值,請在 Python NumPy 中使用 **numpy.fabs()** 方法。此函式返回 x 中資料的絕對值(正幅值)。不處理複數值,使用 absolute 來查詢複數資料的絕對值。
out 是將結果儲存到的位置。如果提供,則其形狀必須是輸入廣播到的形狀。如果未提供或為 None,則返回一個新分配的陣列。元組(僅可能作為關鍵字引數)的長度必須等於輸出的數量。
步驟
首先,匯入所需的庫 -
import numpy as np
使用 array() 方法建立一個浮點型別的陣列 -
arr = np.array([76.7, 28.5, 91.4, -100.8, -120.2, 150.4, 200.7])
顯示陣列 -
print("Array...
", arr)獲取陣列的型別 -
print("
Our Array type...
", arr.dtype)
獲取陣列的維度 -
print("
Our Array Dimension...
",arr.ndim)獲取陣列的形狀 -
print("
Our Array Shape...
",arr.shape)
要返回浮點數的絕對值,請在 Python NumPy 中使用 numpy.fabs() 方法 -
print("
Result...
",np.fabs(arr))示例
import numpy as np
# Create an array with float type using the array() method
arr = np.array([76.7, 28.5, 91.4, -100.8, -120.2, 150.4, 200.7])
# 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 Dimension...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# To return the absolute value of float values, use the numpy.fabs() method in Python Numpy
print("
Result...
",np.fabs(arr))輸出
Array... [ 76.7 28.5 91.4 -100.8 -120.2 150.4 200.7] Our Array type... float64 Our Array Dimension... 1 Our Array Shape... (7,) Result... [ 76.7 28.5 91.4 100.8 120.2 150.4 200.7]
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP