在NumPy中逐元素返回輸入陣列的以10為底的對數


要逐元素返回輸入陣列的以10為底的對數,請在Python NumPy中使用**numpy.log10()**方法。對於實值輸入資料型別,log10始終返回實數輸出。對於無法表示為實數或無窮大的每個值,它會產生nan並設定無效浮點錯誤標誌。

逐元素返回x的以10為底的對數。當x為負數時,返回NaN。如果x是標量,則這是一個標量。

out是儲存結果的位置。如果提供,它必須具有輸入廣播到的形狀。如果不提供或為None,則返回一個新分配的陣列。元組(只能作為關鍵字引數)的長度必須等於輸出的數量。

步驟

首先,匯入所需的庫:

import numpy as np

使用array()方法建立一個數組:

arr = np.array([1e-15, 10000])

顯示陣列:

print("Array...
", arr)

獲取陣列的型別:

print("
Our Array type...
", arr.dtype)

獲取陣列的維數:

print("
Our Array Dimension...
",arr.ndim)

獲取陣列的形狀:

print("
Our Array Shape...
",arr.shape)

要逐元素返回輸入陣列的以10為底的對數,請使用numpy.log10()方法。對於實值輸入資料型別,log10始終返回實數輸出。對於無法表示為實數或無窮大的每個值,它會產生nan並設定無效浮點錯誤標誌:

print("
Result...
",np.log10(arr))

示例

import numpy as np

# Create an array using the array() method
arr = np.array([1e-15, 10000])

# 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 base 10 logarithm of the input array, element-wise, use the numpy.logaddexp() method in Python Numpy # For real-valued input data types, log10 always returns real output. # For each value that cannot be expressed as a real number or infinity, # it yields nan and sets the invalid floating point error flag. print("
Result...
",np.log10(arr))

輸出

Array...
[1.e-15 1.e+04]

Our Array type...
float64

Our Array Dimension...
1

Our Array Shape...
(2,)

Result...
[-15. 4.]

更新於:2022年2月8日

1K+ 次檢視

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告