Method of using logarithmic function in numpy

First introduce a constant ee in numpye is the natural base.

import numpy as np
np.e

Result:
Insert picture description here
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 that log(exp(x)) = x. The natural logarithm is logarithm in base e.

The parameter we input can be a number or an array.

1. Input number:

np.log(1)

Output:
Insert picture description here

2. Input array:

x=[1,  np.e,  np.e**2,  0]
np.log(x)

Output:
Insert picture description here
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!
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43391414/article/details/112340970