Python Natural Log: Calculate ln in Python

Python Natural Log Cover Image

In this tutorial, you’ll learn how to calculate the natural log in Python, thereby creating a way to calculate the mathematical values for ln(). You’ll receive a brief overview of what the natural logarithm is, how to calculate it in Python with the math library and with the numpy library. Finally, you’ll learn how to import it differently to make your code a little easier to read.

The Quick Answer: Use numpy.log()

What is the natural logarithm?

The natural logarithm is the logarithm of any number to the base e. This is often written either as loge(x) or ln(x). Sometimes, the e is implicit, and the function is written as log(x).

The natural logarithm has a number of unique attributes, such as:

  • ln(e) = 1
  • ln(1) = 0

The natural logarithm (ln) is often used in solving time and growth problems. Because the phenomenon of the logarithm to the base e occurs often in nature, it is called the natural logarithm, as it mirrors many natural growth problems.

How to Use Python math to Calculate the Natural Logarithm (ln)

The Python library, math, comes with a function called log(). The function takes two parameters:

  1. The value that you want to calculate the logarithm for, and
  2. The base to use.

An interesting aspect of this function is that the base is an optional value. If no value is provided, the value defaults to e, meaning that by only provided a number, you are automatically calculating the natural logarithm.

This may seem counterintuitive – however, recall from the introduction that in many cases the base e is implicit, and many times is omitted when referring to a log() function without a specified base.

Let’s see how we can use the Python math library to calculate the natural log. We’ll verify some of the key attributes to see how this works in practise.

# Calculate the natural log in Python with math.log
import math

print(math.log(math.e))
print(math.log(1))
print(math.log(10))

# Returns:
# 1.0
# 0.0
# 2.302585092994046

In the next section, you’ll learn how to use the numpy library to calculate the natural logarithm in Python.

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

How to Use Python numpy to Calculate the Natural Logarithm (ln)

Another helpful way in Python to calculate the natural log is to the use the popular numpy library. The numpy library comes with many different ways in which you can manipulate numerical data. One of these functions is the numpy.log() function.

Similar to the function you learned in the previous section, the numpy.log() function takes two parameters:

  1. The number to calculate the logarithm for,
  2. The base to use in the logarithm calculation

Similar to the math library’s function, the base is an optional parameter. If it is left blank, the value of e is used. Because of this, the function defaults to calculating the natural logarithm.

Let’s see how we can use the numpy.log() function to calculate the natural logarithm in Python:

# Calculate the natural log in Python with numpy.log
import numpy as np
import math

print(np.log(math.e))
print(np.log(1))
print(np.log(10))

# Returns:
# 1.0
# 0.0
# 2.302585092994046

In the next section, you’ll learn how to import the log() function in a different manner to make it easier to read.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

How to Import the Log Function to Make It Clearer as ln

Python makes importing functions easy and intuitive. In both examples, we’ve simply imported the whole library, but importing the log() function may not make it clear that we’re referring to natural logs.

One thing that we can do is provide an alias for the function, to make it clear that we are referring to a natural logarithm.

Let’s see how we can do this, by importing the function from the numpy library:

# Calculate the natural log in Python with numpy.log as ln
from numpy import log as ln
import math

print(ln(math.e))
print(ln(1))
print(ln(10))

# Returns:
# 1.0
# 0.0
# 2.302585092994046

In the next section, you’ll learn how to graph the natural log function using Python.

Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!

How to Graph the Natural Log Function in Python

In this section, you’ll learn how to plot the natural log function in Python using the popular graphing library, matplotlib.

In order to plot the data, what we’ll do is

  1. Generate an array of the numbers from 1 through to 30.
  2. We will then loop over the array and create an array of the natural log of that number.
  3. Finally, we will plot the two arrays using matplotlib.

Let’s see how we can do this in Python:

# Calculate the natural log in Python with numpy.log as ln
import numpy as np
import math
import matplotlib.pyplot as plt

x = np.array(range(1, 1001))
y = np.log(x)

plt.plot(x, y)
plt.title('Plotting y=ln(x) with matplotlib')
plt.show()

This returns the following image:

Python Natural Log Plotting Function

Want to learn more about calculating the square root in Python? Check out my tutorial here, which will teach you different ways of calculating the square root, both without Python functions and with the help of functions.

Conclusion

In this tutorial, you learned how to use Python to calculate the natural logarithm. You learned how to do this using both the math and numpy libraries, as well how to plot the natural log function using matplotlib.

To learn more about the math.log() function, check out the official documentation here.