Python

How to Compute the Natural Logarithm (ln) in Python

By ayed_amira , on 08/22/2021 - 3 minutes to read
natural log python

Natural log python: In this tutorial we will see how to compute the natural logarithm using the math and numpy libraries.

Introduction

The natural logarithm (often abbreviated ln) is the inverse of the exponential function. It is often used in mathematics for calculations involving time and growth rates.

In python, several libraries allow us to compute it like the math library and the numpy library. In this tutorial we will see the following elements:

  • Compute the natural logarithm using math
  • Compute the natural logarithm using numpy
  • Graphical representation using matplotlib
  • Calculate the logarithm in base 2
  • Calculate the logarithm in base 10

Let’s start with the math library.

Natural log python

Compute the natural log using math

The math library has a function called math.log(x) which returns the natural logarithm of x :

import math

nlog1 = math.log(1)
print(nlog1)
nlog5 = math.log(5)
print(nlog5)

#Output:

>>>0.0
>>>1.6094379124341003

Compute the natural log using numpy

The NumPy library also has a function called numpy.log() which allows the user to compute the natural logarithm of x where x belongs to all the elements of the input array.

import numpy as np

input = [1, 100, 500, 20000]
print("Input: ", input)

output = np.log(input)
print("Output: ", output)

print("np.log(5) : ", np.log(5))

#Output:

>>>Input:  [1, 100, 500, 20000]
>>>Output:  [0. 4.60517019 6.2146081  9.90348755]
>>>np.log(5) :  1.6094379124341003

Graphical representation using matplotlib

It is possible to graphically represent the results provided by the matplotlib library:

from matplotlib import pyplot as plt
import numpy as np

input = [1, 2, 3, 4, 5, 6, 7]
output = np.log(input)

plt.plot(input, input,
        color='blue', marker="*")

# red for numpy.log()
plt.plot(output, input,
         color='green', marker="o")

plt.title("numpy.log() - Graphical representation using matplotlib")
plt.xlabel("output")
plt.ylabel("input")
plt.show()

Here is the graphic representation:

natural log python - graphic representation
Graphical representation – numpy.log()

Calculate the logarithm in base 2

If you want to use the logarithm in base 2, you can use the function numpy.log2(x) :

import numpy as np

print(np.log2(1))
print(np.log2(5))
print(np.log2(10))

#Output:

>>>0.0
>>>2.321928094887362
>>>3.321928094887362

Calculate the logarithm in base 10

If you want to use the logarithm in base 10, you can use the function numpy.log10(x) :

import numpy as np

print(np.log10(1))
print(np.log10(5))
print(np.log10(10))

#Output:

>>>0.0
>>>0.6989700043360189
>>>1.0

Conclusion

In this tutorial, we have seen how to calculate the natural logarithm in python using the NumPy and math libraries. These functions are widely used in mathematics and are quite easy to use.

If you don’t understand how to use them, don’t hesitate to tell me in the comments, I’m available to answer your questions.

See you soon for new tutorials !

Back to Python Menu

ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Comments

Leave a comment

Your comment will be revised by the site if needed.