Numpy中輸入指數和的對數(以2為底)
要獲取輸入指數和的對數(以2為底),可以使用Python Numpy中的**numpy.logaddexp()**方法。
計算**log2(2**x1 + 2**x2)**。此函式在機器學習中很有用,當計算出的事件機率可能非常小,以至於超出普通浮點數的範圍時。在這種情況下,可以使用計算出的機率的以2為底的對數。此函式允許以這種方式儲存的機率相加。
NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等。它支援各種硬體和計算平臺,並且與分散式、GPU 和稀疏陣列庫配合良好。
步驟
首先,匯入所需的庫 -
import numpy as np
以2為底的對數輸入 -
one = np.log2(2e-50) two = np.log2(3.2e-50)
顯示對數輸入 -
print("Value 1...
", one)
print("Value 2...
", two)要獲取輸入指數和的對數(以2為底),可以使用numpy.logaddexp()方法 -
res = np.logaddexp(one, two)
print("
Logarithm of the sum of exponentiations of the inputs in base 2...
",res)示例
import numpy as np
# Calculates log2(2**x1 + 2**x2).
# This function is useful in machine learning when the calculated probabilities of events may be so small
# as to exceed the range of normal floating point numbers.
# In such cases the base-2 logarithm of the calculated probability can be used instead.
# This function allows adding probabilities stored in such a fashion.
# Log2 input
one = np.log2(2e-50)
two = np.log2(3.2e-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 in base 2, use the numpy.logaddexp() method in Python Numpy
res = np.logaddexp(one, two)
print("
Logarithm of the sum of exponentiations of the inputs in base 2...
",res)輸出
Value 1... -165.09640474436813 Value 2... -164.41833283925547 Logarithm of the sum of exponentiations of the inputs in base 2... -164.00781734564688
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP