SciPy - Curve Fitting - GeeksforGeeks
SciPy - Curve Fitting - GeeksforGeeks
SciPy - Curve Fitting - GeeksforGeeks
Given a Dataset comprising of a group of points, find the best fit representing the Data.
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.
Using SciPy :
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)
We use cookies to ensure you have the best browsing experience on our website. By using our site,
Got It !
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/scipy-curve-fitting/ 1/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
Output :
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/scipy-curve-fitting/ 2/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
Input :
Output :
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/scipy-curve-fitting/ 3/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
As seen in the input, the Dataset seems to be scattered across a sine function in the first
case and an exponential function in the second case, Curve-Fit gives legitimacy to the
functions and determines the coefficients to provide the line of best fit.
Python3
import numpy as np
Trending Now Data Structures Algorithms Topic-wise Practice Python Machine Learning Data Science J
# curve-fit() function imported from scipy
from scipy.optimize import curve_fit
https://www.geeksforgeeks.org/scipy-curve-fitting/ 4/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
Output:
Second example can be achieved by using the numpy exponential function shown as
follows:
Python3
However, if the coefficients are too large, the curve flattens and fails to provide the best fit.
The following code explains this fact:
Python3
import numpy as np
from scipy.optimize import curve_fit
from
We use matplotlib import
cookies to ensure you havepyplot as pltexperience on our website. By using our site,
the best browsing
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/scipy-curve-fitting/ 5/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
ans = (param[0]*(np.sin(param[1]*x)))
Output:
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.
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/scipy-curve-fitting/ 6/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
Similar Reads
How to do exponential and 3D Curve Fitting With Python
logarithmic curve fitting in
Python?
SciPy Linear Algebra - SciPy Linalg Regression Analysis and the Best
Fitting Line using C++
Related Tutorials
Computer Vision Tutorial Pandas AI: The Generative AI
Python Library
Previous Next
Article Contributed By :
ArijitGayen
A ArijitGayen
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/scipy-curve-fitting/ 7/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
Follow
Company
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Explore
https://www.geeksforgeeks.org/scipy-curve-fitting/ 8/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
https://www.geeksforgeeks.org/scipy-curve-fitting/ 9/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
Engineering Maths
Commerce UPSC
Accountancy Polity Notes
Business Studies Geography Notes
Economics History Notes
Management Science and Technology Notes
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge thatIncome
you haveTax
read and understood our Cookie Policy & Privacy Policy Economics Notes
https://www.geeksforgeeks.org/scipy-curve-fitting/ 10/11
8/18/23, 3:31 AM SciPy | Curve Fitting - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/scipy-curve-fitting/ 11/11