Change language

SciPy | Curve Fitting

|

We often have a dataset of data following a common path, but each of the data has a standard deviation that makes it scattered along the line of best fit. We can get one line using the curve-fit () function.

Using SciPy:
Scipy — is a Python scientific computing module that provides built-in functions for many well-known mathematical functions. The scipy.optimize package gives us a lot of optimization routines. A detailed list of all Optimize functions can be found by typing the following in the iPython console:

 help (scipy.optimize) 

Among the most commonly used methods — least squares minimization, curve fitting, multidimensional scalar minimization, etc.

Curve fitting example —

Login :

Output:

Login:

Output:

As you can see from the input, the dataset appears to be scattered across a sinusoidal function in the first case and an exponential function in the second case, Curve-Fit lends legitimacy to the features and determines the coefficients to ensure the line of best fit.

Code showing the generation of the first example —

import numpy as np

 
# curve-fit () function imported from scipy

from scipy.optimize import curve_fit

 

from matplotlib import pyplot as plt

 
# numpy.linspace with given arguments
# create an array 40 numbers between 0
# and 10, both inclusive

x = np.linspace ( 0 , 10 , num = 40 )

  

 
# y is another array that stores 3.45 times
# sine (values ​​in x) * 1.334.
# Random.normal () draws a random sample
# from a normal (Gaussian) distribution to make
# they are scattered along the baseline

y = 3.45 * np.sin ( 1.334 * x) + np.random.norma l (size = 40 )

 
# Test function with coefficients as parameters

def test (x, a, b):

  return a * np. sin (b * x)

 
# curve_fit () function accepts a test function
# x-data and y-data in as an argument and returns
# A and B coefficients in parameters and
# assumed parameter covariance in param_cov

param, param_cov = curve_fit (test, x, y)

  

 

print ( "Sine funcion coefficients:" )

print (param)

print ( "Covariance of coefficients:" )

print (param_cov)

 
# ans stores new y-data according to
# coefficients given by the curve-fit () function

ans = (param [ 0 ]   * (np.sin (param [ 1 ] * x)))

  
& # 39; & # 39; & # 39; Below 4 lines can be commented out to display the results
using matplotlib as shown in the first example. "" "

  
# plt.plot (x, y, & # 39; o & # 39 ;, color = & # 39; red & # 39 ;, label = & quot; data & quot;)
# plt.plot (x, ans, & # 39; - & # 39 ;, color = & # 39; blue & # 39 ;, label = & quot; optimized data & quot;)
# plt.legend ( )
# plt.show ()

Exit:

 Sine function coefficients: [3.66474998 1.32876756] Covariance of coefficients: [[5.43766857e-02 -3.69114170e-05] [-3.69114170e-05 1.02824503e-04]] 

The second example can be achieved with the numpy exponential function shown below:

x = np.linspace ( 0 , 1 , num = 40 )

 

y = 3.45 * np. exp ( 1.334 * x) + np.random.normal (size = 40 )

 

def test (x, a, b):

  return a * np.exp (b * x)

  

param, param_cov = curve_fit (test, x, y)

However, if the coefficients are too high, the curve flattens and does not provide the best fit. The following code explains this fact:

import numpy as np

from scipy.optimize import curve_fit

 

from matplotlib import pyplot as plt

  

x = np.linspace ( 0 , 10 , num = 40 )

 
# The odds are much higher.

y = 10.45 * np.sin ( 5.334 * x) + np.random.normal (size = 40 )

 

def test (x, a, b):

return a * np.sin (b * x)

 

param, param_cov = cur ve_fit (test, x, y)

 

print ( "Sine funcion coefficients:" )

print (param)

print ( "Covariance of coefficients:" )

print (param_cov)

 

ans = (param [ 0 ] * (np.sin ( param [ 1 ] * x)))

  

plt.plot (x, y, ’o’ , color = ’red’ , label = "data" )

plt.plot (x , ans, ’--’ , color = ’blue’ , label = "optimized data" )

plt.legend ()
plt.show ()

Output:

 Sine funcion coefficients: [0.70867169 0.7346216] Covariance of coefficients: [[2.87320136 -0.05245869] [-0.05245869 0.14094361]]   

The blue dashed line is undoubtedly the line with optimally optimized distances from all points in the dataset, but it does not provide the best fit sine function.

Curve fitting should not be confused with regression. Both of them include data approximation with functions. But the purpose of curve fitting is to provide values ​​for a dataset with which a given set of explanatory variables can actually represent another variable. Regression — this is a special case of curve fitting, but here you just don’t want a curve that best fits the training data (which can lead to overfitting), but a model that can generalize the training and thus predict new points. effectively.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically