NumPy: Logarithm with base n

PythonMathNumpyLogarithm

Python Problem Overview


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.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?

Python Solutions


Solution 1 - Python

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondwitvlietView Question on Stackoverflow
Solution 1 - PythondwitvlietView Answer on Stackoverflow