Open In App

SciPy | Curve Fitting

Last Updated : 08 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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