Numpy 中輸入冪和的對數


要獲得輸入冪和的對數,請在 Python Numpy 中使用 numpy.logaddexp() 方法。

計算 log(exp(x1) + exp(x2))。此函式在統計學中很有用,其中計算的事件機率非常小,可能超出正常浮點數的範圍。在這種情況下,儲存計算出的機率的對數。此函式允許以這種方式儲存的機率相加。

NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等。它支援廣泛的硬體和計算平臺,並且與分散式、GPU 和稀疏陣列庫配合得很好。

步驟

首先,匯入所需的庫 -

import numpy as np

登入輸入 -

one = np.log(1e-50)
two = np.log(2.5e-50)

# 顯示日誌輸入 -

print("Value 1...
", one) print("Value 2...
", two)

要獲得輸入冪和的對數,請在 Python Numpy 中使用 numpy.logaddexp() 方法 -

res = np.logaddexp(one, two)
print("
Logarithm of the sum of exponentiations...
",res)

示例

import numpy as np

# Calculates log(exp(x1) + exp(x2)).
# This function is useful in statistics where the calculated probabilities
# of events may be so small as to exceed the range of normal floating point numbers.
# In such cases the logarithm of the calculated probability is stored.
# This function allows adding probabilities stored in such a fashion.

# Log input
one = np.log(1e-50)
two = np.log(2.5e-50)

# Display the log input
print("Value 1...
", one) print("Value 2...
", two) # To get the Logarithm of the sum of exponentiations of the inputs, use the numpy.logaddexp() method in Python Numpy res = np.logaddexp(one, two) print("
Logarithm of the sum of exponentiations...
",res)

輸出

Value 1...
-115.12925464970229
Value 2...
-114.21296391782813

Logarithm of the sum of exponentiations...
-113.87649168120691

更新日期:07-Feb-2022

137 瀏覽量

開啟您的職業生涯

完成課程以獲得認證

開始
廣告