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 NumPy log base The Numpy log() function offers a possibility of finding logarithmic values concerning user-defined bases. The natural logarithm is the reverse of the exponential function so that log of the exponential of X will give you the X so the logarithm in base E is called the natural logarithm.
log. Natural logarithm, element-wise. The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x.
log2() in Python. numpy. log2(arr, out = None, *, where = True, casting = 'same_kind', order = 'K', dtype = None, ufunc 'log1p') : This mathematical function helps user to calculate Base-2 logarithm of x where x belongs to all the input array elements.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With