HOWTO · Python
How to Do Exponential and Logarithmic Curve Fitting in Python
This tutorial demonstrates how to do exponential and logarithmic curve fitting in Python.
On this page
Curve fitting is a very efficient tool that is vastly used for analysis. The curve fitting method studies the relationship between independent variables that are also known as predictors and dependent variables known as response variables. This method aims to provide the most suitable model to fit a certain amount of data points.
There are two types of curve fitting:
- Logarithmic Curve Fitting
- Exponential Curve Fitting
In this tutorial, we will show you methods on how to do logarithmic curve fitting and exponential curve fitting in Python. Both use the same trick: take the logarithm of one variable so the model becomes a straight line, then fit that line with a least-squares polynomial fit.
Libraries and Modules Used for Logarithmic and Exponential Curve Fitting in Python
Let’s discuss the possible libraries and modules you can use to execute programs.
the NumPy Library
Functions that we will be using from the NumPy library are the following.
array()- This function is used to create aNumPyarray that is a set of the same type of values and has index value in the form of tuples.log()- This function is more of a mathematical operation that helps calculate the natural logarithmic of a number. That number is a part of the elements of the input array.exp()- This function is also a mathematical operation used to calculate the exponential of elements present in an inputNumPyarray.polyfit()- This function helps in fitting any data in a polynomial function. It least squares a polynomial fit.
the Matplotlib Library
The Matplotlib library is mostly used for plotting in Python. This library is generally used for creating visualizations in Python. A module from this library known as the pyplot module is used in this tutorial.
The pyplot module of the Matplotlib library is an open-source module that helps make the Matplotlib library work like MATLAB. This module has many functions that help us carry out operations like creating a plotting area, creating labels on a plot, and more.
the Logarithmic Curve Fitting
A logarithmic model has the form y = a * log(x) + b. Because the model is linear in log(x), you can fit it by running a straight-line fit of y against log(x) — polyfit with degree 1 does exactly that and returns a and b:
import numpy as np
x = np.array([5, 10, 15, 20, 25])
y = np.array([3, 6, 9, 12, 15])
log_x = np.log(x)
coefficients = np.polyfit(log_x, y, 1)
print(coefficients)
Output:
[ 7.2647162 -9.64806344]
So the fitted model is y = 7.2647 * log(x) - 9.6481. For plotting, draw the data and the fitted line against log(x):
import numpy as np
import matplotlib.pyplot as plt
x = np.array([5, 10, 15, 20, 25])
y = np.array([3, 6, 9, 12, 15])
log_x = np.log(x)
slope, intercept = np.polyfit(log_x, y, 1)
fitted = slope * log_x + intercept
plt.plot(log_x, y, "o")
plt.plot(log_x, fitted)
plt.show()
Output:
In the program above, we first import the necessary libraries and create two NumPy arrays as our primary data. We take the logarithm of the x values only — the model is linear in log(x), so y itself is not transformed. The polyfit() function then fits a straight line to y against log(x), and its two returned values are the coefficients of the logarithmic equation.
Note that the sample data above grows linearly in x, so treat this as a demonstration of the procedure; on real data, check how well the fitted curve actually matches before relying on the model.
the Exponential Curve Fitting
An exponential model has the form y = c * e^(d * x). Taking the natural log of both sides gives log(y) = log(c) + d * x, which is again a straight line — this time of log(y) against x. Fit that line, and the intercept gives you log(c):
import numpy as np
a = np.array([6, 12, 18, 24, 30])
b = np.array([4, 8, 12, 16, 20])
log_b = np.log(b)
coefficients = np.polyfit(a, log_b, 1)
print(coefficients)
Output:
[0.06520038 1.17018581]
The slope is d = 0.0652 and the intercept is log(c) = 1.1702, so the fitted model is b = e^1.1702 * e^(0.0652 * a). For plotting, evaluate that model at the data points:
import numpy as np
import matplotlib.pyplot as plt
a = np.array([6, 12, 18, 24, 30])
b = np.array([4, 8, 12, 16, 20])
slope, intercept = np.polyfit(a, np.log(b), 1)
fitted = np.exp(intercept) * np.exp(slope * a)
plt.plot(a, b, "o")
plt.plot(a, fitted)
plt.show()
Output:
The same procedure is followed as we did in the logarithmic curve fitting, mirrored: there we transformed the input (log(x)), here we transform the output (log(b)) and then undo the transformation with exp() when evaluating the fitted model. As with the logarithmic example, the sample data is linear, so this demonstrates the mechanics of the exponential fit rather than a case where the exponential model is the natural choice.