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
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:
Among the most used are Least-Square minimization, curve-fitting, minimization of multivariate scalar functions etc.
Curve Fitting Examples –
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.
Code showing the generation of the first example –
Sine function coefficients: [ 3.66474998 1.32876756] Covariance of coefficients: [[ 5.43766857e-02 -3.69114170e-05] [ -3.69114170e-05 1.02824503e-04]]
Second example can be achieved by using the numpy exponential function shown as follows:
However, if the coefficinets are too large, the curve flattens and fails to provide the best fit. The following code explains this fact:
Sine funcion coefficients: [ 0.70867169 0.7346216 ] Covariance of coefficients: [[ 2.87320136 -0.05245869] [-0.05245869 0.14094361]]
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 which 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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- SciPy - Integration of a Differential Equation for Curve Fit
- Koch Curve or Koch Snowflake
- Precision-Recall Curve | ML
- Python - Hilbert Curve using turtle
- Plotting the Growth Curve of Coronavirus in various Countries using Python
- Python - Sympy Curve.translate() method
- Validation Curve
- PyCairo- Drawing Function curve
- PyCairo - Drawing Bezier curve
- sciPy stats.tsem() function | Python
- sciPy stats.tvar() function | Python
- sciPy stats.gmean() function | Python
- sciPy stats.hmean() | Python
- scipy stats.tmin() function | Python
- sciPy stats.tstd() function | Python
- sciPy stats.tmean() | Python
- sciPy stats.tmax() function | Python
- sciPy stats.describe() function | Python
- sciPy stats.mean() function | Python
- sciPy stats.nanmedian() function | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.