Python NumPy:以 n 为底的对数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25169297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:49:42  来源:igfitidea点击:

NumPy: Logarithm with base n

pythonmathnumpylogarithm

提问by Banana

From the numpy documentation on logarithms, I have found functions to take the logarithm with base e, 2, and 10:

关于 logarithmsnumpy 文档中,我找到了以e210为底取对数的函数:

import numpy as np
np.log(np.e**3) #3.0
np.log2(2**3)   #3.0
np.log10(10**3) #3.0

However, how do I take the logarithm with base n(e.g. 42) in numpy?

但是,如何在 numpy 中取以n(例如 42)为底的对数?

采纳答案by Banana

To get the logarithm with a custom base using math.log:

要使用自定义基数获取对数,请使用math.log

import math
number = 74088  # = 42**3
base = 42
exponent = math.log(number, base)  # = 3

To get the logarithm with a custom base using numpy.log:

要使用自定义基数获取对数,请使用numpy.log

import numpy as np
array = np.array([74088, 3111696])  # = [42**3, 42**4]
base = 42
exponent = np.log(array) / np.log(base)  # = [3, 4]

As you would expect, note that the default case of np.log(np.e) == 1.0.

正如您所料,请注意np.log(np.e) == 1.0.



As a reminder, the logarithm base changerule is:

提醒一下,对数基数变化规则是:

\log_b(x)=\log_c(x)/\log_c(b)

\log_b(x)=\log_c(x)/\log_c(b)