NumPy: 以 n 为底的对数

关于对数的 numpy 文档中,我找到了以 [俄语]210为基数的对数函数:

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

但是,如何取对数的基数 N(例如42)在 numpy?

118274 次浏览

To get the logarithm with a custom base using 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:

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

Which uses the logarithm base change rule:

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