Generating The Best Fit Curve In Python Using Numpy and Matplotlib
Updated: Aug 25
Fitting obtained data to a best-fit curve is an essential part of data analysis and data extrapolation. Generally, this is performed post-data regression.
In this lesson, we are working with the data shown in the table below
x | y |
0.3 | 0.1 |
0.5 | 0.6 |
0.9 | 1.1 |
1.3 | 1.8 |
1.2 | 1.5 |
1.9 | 2.2 |
3.2 | 4.3 |
2.9 | 3.9 |
4 | 5 |
4.9 | 5.8 |
Before we begin working on fitting the curve, we must understand what kind of curve we expect to find. For the sake of this example, we shall be fitting this curve to the form:
y = a*exp(b*x)
But before we fit the data into a curve, we can only fit it as a linear or quadratic equation of the form:
y = a*x + b
y = a*x**2 + b*x + c
Thus, we must convert the exponential equation to a linear one, using logarithms, as shown:
y = a*exp(b*x)
ln(y) = ln(a) + ln(exp(b*x))
Y = A + b*x
(or)
Y = b*x + A
Where, b is the slope of the linear equation, A is the intercept, a = exp(A) and y = exp(y)
Now that we have understood our equation, we will convert our data from the table into matrices as:
x1 = [0.3, 0.5, 0.9, 1.3, 1.2, 1.9, 3.2, 2.9, 4, 4.9]
y1 = [0.1, 0.6, 1.1, 1.8, 1.5, 2.2, 4.3, 3.9, 5, 5.8]
In order for us to fit the curve, we work with the numpy's polyfit() function. It converts the data of x and y coordinates into the best-fit curve based on the order of the equation we provide.
1 for linear...
2 for quadratic...
3 for cubic...
And so on.
The polyfit() function's syntax is as follows:
curve = polyfit(X_COORDINATE, Y_COORDINATE, ORDER)
The polyfit() function returns an array of the coefficients of the equation with the number of elements in the array equalling the number of coefficients of the equation. X_COORDINATE and Y_COORDINATE arguments are arrays of the coordinates.
We will now begin with the development of our program.
from numpy import *
import math
from matplotlib import pyplot
Plot = pyplot.plot
Show = pyplot.show
x1 = [0.3, 0.5, 0.9, 1.3, 1.2, 1.9, 3.2, 2.9, 4, 4.9]
y1 = [0.1, 0.6, 1.1, 1.8, 1.5, 2.2, 4.3, 3.9, 5, 5.8]
si = polyfit(x1, y1, 1)
si = exp(si)
print(si)
x = linspace(0, 10, 2000)
y = si[0]*exp(si[1]*x)
Plot(x, y)
Show()
Once you run the program, you will be shown the data of the best-fit exponential curve and the graph will be displayed in the window.
Output:
[3.50634266 0.98299611]