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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP