Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumPy: Logarithm with base n

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?

like image 661
dwitvliet Avatar asked Aug 06 '14 20:08

dwitvliet


People also ask

What is the base of log 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.

How do you write natural log in Numpy?

log. Natural logarithm, element-wise. The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x.

How do you do log base 2 in Numpy?

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.


1 Answers

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)

like image 73
dwitvliet Avatar answered Oct 21 '22 20:10

dwitvliet