Summary of logarithm method in Python

Both the Math library and the Python library in Python have functions for finding logarithms.

import numpy as np
import math

1. Numpy library

1.1 Find the logarithm to base e, 2, 10

Function function
np.log(x) Logarithm based on e (natural logarithm)
np.log10(x) Log base 10
np.log2(x) Logarithm to base 2
np.log1p(x) Equivalent to: np.log(x + 1)

Note: np.expm1(x) is equivalent to np.exp(x) – 1, which is also the inverse operation of np.log1p(x).

1.2 Find the base logarithm of any number

In Numpy, the logarithm based on any number needs to use the base exchange formula:

For example: base 3, logarithm of 5

The code is written as:

np.log(5)/np.log(3)

2. Math library

2.1 Find the logarithm to base e, 2, 10

Exactly the same as the usage in Numpy

Function function
math.log(x) Logarithm based on e (natural logarithm)
math.log10(x) Logarithm to base 10
math.log2(x) base 2 logarithm
math.log1p(x) Equivalent to: math.log(x + 1), used for data smoothing

Note: math.expm1(x) is equivalent to math.exp(x) – 1, which is also the inverse operation of math.log1p(x).

2.2 Find the base logarithm of any number

math.log(x, n)

Where n is the base

3. the difference

Why is there a method to find the logarithm in the Math library, but the same function is built in the Numpy library?

the reason:

In the math library, the input x of the function can only be a single number.

math.log10(100)[out]:2.0

If the input is a list:

math.log10([10,100])   #Will report an error
TypeError: must be real number, not list

In the Numpy library, the input x of the function can be not only a single number, but also a list or a Numpy array.

np.log10([10,100])[out]:array([1.,2.])

np.log10([[10,100],[1000,10000]])[out]:array([[1.,2.],[3.,4.]])

The result is a Numpy array. That is to say, the functions in Numpy can realize batch processing of each element without looping.

So far this article on the summary of the logarithm method in Python is introduced. For more related Python logarithm content, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. I hope you will support ZaLou more in the future. Cn!

Recommended Posts

Summary of logarithm method in Python
Method of installing django module in python
Subscripts of tuples in Python
Python method of parameter passing
Use of Pandas in Python development
Use of numpy in Python development
Detailed usage of dictionary in Python
Usage of os package in python
A summary of 200 Python standard libraries!
Summary of Python calling private attributes
The usage of tuples in python
End the method of running python
Description of in parameterization in python mysql
Understanding the meaning of rb in python
Implementation of JWT user authentication in python
The usage of Ajax in Python3 crawler
Comprehensive summary of Python built-in exception types
Analysis of glob in python standard library
Summary of common operations of Python time module
Summary of knowledge points about Python unpacking
The specific method of python instantiation object
Example method of reading sql from python
Knowledge points of shell execution in python
The specific method of python import library
Functions in python
7 features of Python3.9
Detailed explanation of the use of pip in Python | summary of third-party library installation
Python basic summary
Method analysis of Python calling C language program
What is the function of adb in python
Detailed explanation of the principle of Python super() method
Summary of CentOS method to set static IP
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
The consequences of uninstalling python in ubuntu, very
Example of feature extraction operation implemented in Python
Detailed explanation of static DNS configuration method in Ubuntu
Python magic method topic
03. Operators in Python entry
Python object-oriented magic method
A brief summary of the difference between Python2 and Python3
Python3.7 debugging example method
Python processing json summary
Join function in Python
12. Network Programming in Python3
print statement in python
Installation and use of GDAL in Python under Ubuntu
Python interview questions summary
Python function-dictionary get() method
How to understand the introduction of packages in Python
Basics of Python syntax
How to understand a list of numbers in python
Concurrent requests in Python
Python advanced usage summary
Basic syntax of Python
Basic knowledge of Python (1)
Install python in Ubuntu
Example of how to automatically download pictures in python
Context management in Python
Prettytable module of python
Arithmetic operators in python