We often have a dataset comprising of data following a general path, but each data has a standard deviation which makes them scattered across the line of best fit. We can get a single line using curve-fit() function. So given a dataset comprising of a group of points, Curve Fitting helps to find the best fit representing the Data.
Scipy is the scientific computing module of Python providing in-built functions on a lot of well-known Mathematical functions. The scipy.optimize package equips us with multiple optimization procedures. A detailed list of all functionalities of Optimize can be found on typing the following in the iPython console:
help(scipy.optimize)
Among the most used are Least-Square minimization, curve-fitting, minimization of multivariate scalar functions etc.
Curve Fitting Examples
Example 1: Sine Function
Input :

Code Implementation:
Python
import numpy as np
from scipy.optimize import curve_fit
from matplotlib import pyplot as plt
# Generate 40 values between 0 and 10
x = np.linspace(0, 10, num=40)
# Generate noisy sine data
y = 3.45 * np.sin(1.334 * x) + np.random.normal(size=40)
# Sine function model
def test(x, a, b):
return a * np.sin(b * x)
# Fit the model to the data
param, param_cov = curve_fit(test, x, y)
print("Sine function coefficients:")
print(param)
print("Covariance of coefficients:")
print(param_cov)
# Generate fitted values using the optimized parameters
ans = param[0] * np.sin(param[1] * x)
# Plot original and fitted data
plt.plot(x, y, 'o', color='red', label="data")
plt.plot(x, ans, '--', color='blue', label="optimized data")
plt.legend()
plt.show()
Output :

Sine function coefficients:
[3.43157585 1.33848481]
Covariance of coefficients:
[[ 4.24498442e-02 -5.39500036e-05]
[-5.39500036e-05 9.25416596e-05]]
2. Example 2: Exponential Function
Input :

Code Implementation:
Python
import numpy as np
from scipy.optimize import curve_fit
from matplotlib import pyplot as plt
# Generate x values between 0 and 1
x = np.linspace(0, 1, num=40)
# Generate y values using exponential function with added noise
y = 3.45 * np.exp(1.334 * x) + np.random.normal(size=40)
# Exponential function model
def test_exp(x, a, b):
return a * np.exp(b * x)
# Fit model to data
param, param_cov = curve_fit(test_exp, x, y)
# Print optimized parameters and their covariance
print("Exponential function coefficients:")
print(param)
print("Covariance of coefficients:")
print(param_cov)
# Generate fitted y values
ans = param[0] * np.exp(param[1] * x)
# Plot original data and fitted curve
plt.plot(x, y, 'o', color='red', label='Noisy data')
plt.plot(x, ans, '--', color='blue', label='Fitted curve')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Exponential Curve Fitting')
plt.legend()
plt.show()
Output :

Exponential function coefficients:
[3.75594702 1.26908706]
Covariance of coefficients:
[[ 0.04559006 -0.01531584]
[-0.01531584 0.00581957]]
The blue dotted line is undoubtedly the line with best-optimized distances from all points of the dataset, but it fails to provide a sine function with the best fit.
Curve Fitting should not be confused with Regression. They both involve approximating data with functions. But the goal of Curve-fitting is to get the values for a Dataset through which a given set of explanatory variables can actually depict another variable. Regression is a special case of curve fitting but here you just don't need a curve that fits the training data in the best possible way(which may lead to overfitting) but a model which is able to generalize the learning and thus predict new points efficiently.
Similar Reads
3D Curve Fitting With Python Curve fitting is a widely used technique in the field of data analysis and mathematical modeling. It involves the process of finding a mathematical function that best approximates a set of data points. In 3D curve fitting, the process is extended to three-dimensional space, where the goal is to find
7 min read
3D Curve Fitting With Python Curve fitting is a widely used technique in the field of data analysis and mathematical modeling. It involves the process of finding a mathematical function that best approximates a set of data points. In 3D curve fitting, the process is extended to three-dimensional space, where the goal is to find
7 min read
Validation Curve Model validation is an important part of the data science project since want to select a model which not only performs well on our training dataset but also has good accuracy on the testing dataset. Model validation helps us in finding a model which has low variance. What is Validation Curve  A Val
4 min read
Using Learning Curves - ML A learning model of a Machine Learning model shows how the error in the prediction of a Machine Learning model changes as the size of the training set increases or decreases. Before we continue, we must first understand what variance and bias mean in the Machine Learning model. Bias: It is basically
4 min read
SciPy - Integration of a Differential Equation for Curve Fit In Machine Learning, often what we do is gather data, visualize it, then fit a curve in the graph and then predict certain parameters based on the curve fit. If we have some theoretical data we can use curve fitting from the verified dataset to extract the equation and verify it. So to find the equa
2 min read
Cubic spline Interpolation Cubic Spline Interpolation is a method used to draw a smooth curve through a set of given data points. Instead of connecting the points with straight lines or a single curve, it fits a series of cubic polynomials between each pair of points. These polynomials join smoothly, making the curve look nat
4 min read