在 NumPy 中返回輸入陣列的逐元素自然對數加一


要返回輸入陣列的逐元素自然對數加一,請在 Python NumPy 中使用 **numpy.log1p()** 方法。它計算 log(1 + x)。

對於複數值輸入,log1p 是一個複雜的解析函式,它有一個分支切割 [-inf, -1],並且在其上從上方連續。log1p 將浮點負零處理為無窮小的負數,符合 C99 標準。

步驟

首先,匯入所需的庫:

import numpy as np

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

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

顯示陣列:

print("Array...
", arr)

獲取陣列的型別:

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

獲取陣列的維度:

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

獲取陣列的形狀:

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

要返回輸入陣列的逐元素自然對數加一,請使用 numpy.log1p() 方法。計算 log(1 + x):

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

示例

import numpy as np

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

# 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 natural logarithm of one plus the input array, element-wise., use the numpy.log1p() method in Python Numpy # Calculates log(1 + x). print("
Result...
",np.log1p(arr))

輸出

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

Our Array type...
float64

Our Array Dimension...
1

Our Array Shape...
(3,)

Result...
[1.00000000e-15 9.21044037e+00 1.00000000e-99]

更新於:2022年2月8日

140 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告