NumPy: Logarithm with base n

后端 未结 1 1754
时光说笑
时光说笑 2020-12-01 10:18

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

import numpy as np
np.log(np.e**3) #3.0
np.l         


        
相关标签:
1条回答
  • 2020-12-01 10:47

    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]
    

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


    As a reminder, the logarithm base change rule is:

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

    0 讨论(0)
提交回复
热议问题