First introduce a constant ee in numpye is the natural base.
import numpy as np
np.e
Result:
Then we started to use the logarithmic function np.log()
. It should be noted that this logarithmic function is based on eeThe logarithmic function with e as the base, that is, this is a natural logarithmic operation.
The natural logarithm
log
is the inverse of the exponential function,so thatlog(exp(x)) = x
. The natural logarithm is logarithm in basee
.
The parameter we input can be a number or an array.
1. Input number:
np.log(1)
Output:
2. Input array:
x=[1, np.e, np.e**2, 0]
np.log(x)
Output:
the fourth one represents − ∞ -\infty−∞
A question is here, what if you want to perform logarithmic function operations with other bases (such as 2, 10)?
Can be achieved indirectly, because there are
log m n = log e n log e m \log_mn=\frac{\log_en}{\log_em} logmn=logemlogen
So we define the function as follows:
def log(base,x):
return np.log(x)/np.log(base)
Then, if we want to calculate log 2 8 \log_28log28:
log(2,8)
Output 3, and you're done!