NumPy Log

In this tutorial you will learn:

  • What is a logarithm?
  • Implementation of Log with different bases in Python
  • Python Syntax

Logarithm

In order to simplify the calculations, Logarithms were introduced in 1614 by John Napier. Logarithm is inverse to an exponential function. Lets take a number x, its logarithm is the exponent to which another fixed number, the base b must be raised to produce x. The logarithm with base 10 is called as the common logarithm and it has many applications in the domain of engineering and science. There are other logarithms as well with different bases used in mathematics, physics and sciences.

Implementation of Logarithms with different bases in Python

In NumPy there are following 3 functions for the implementation of logarithms
  • log()
  • log2()
  • log10()
We can also create a log function for any base as per the requirement of the programmer. We will comprehend each of the function using the example for each. In each example we will be taking the log of different numbers with different bases, however we can also take the logs of complete arrays with a single line of code. log() function is used to take the natural log or log at base e of any input number. In this example we are taking the natural log of 2.23
  1. #importing the numpy lib
  2. import numpy as n
  3. #Declaring a variable num with value 2.23
  4. num=2.23
  5. #taking natural log of 2.23 using log function
  6. print('Result of taking natural log of 2.23:')
  7. print(n.log(num))
log10() is the function which is used to take the log at the base 10 of the input number. In this example we are taking log to the base 10 of 1.98
  1. #importing the numpy lib
  2. import numpy as n
  3. #Declaring a variable 'num' with value 1.98
  4. num=1.98
  5. #taking log at base 10 of 1.98 using log10() function
  6. print('Result of taking log at base 10 of 1.98:')
  7. print(n.log10(num))
In this example we will be taking log at base 2 using function log2() of the number 5.44
  1. #importing the numpy lib
  2. import numpy as n
  3. #Declaring a variable 'num' with value 5.44
  4. num=5.44
  5. #taking log at base 2 of 5.44 using log2() function
  6. print('Result of taking log at base 2 of 5.44:')
  7. print(n.log2(num))

Add new comment