Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - R square and absolute sum of squares obtainable by scipy.optimize curve_fit?

I am fitting curves using curve_fit. Is there a way to read out the coefficient of determination and the absolute sum of squares? Thanks, Woodpicker

like image 875
Woodpicker Avatar asked May 09 '13 08:05

Woodpicker


People also ask

What does SciPy optimize curve_fit do?

The Python SciPy Optimize Curve Fit function is widely used to obtain the best-fit parameters. The curve_fit() function is an optimization function that is used to find the optimized parameter set for a stated function that perfectly fits the provided data set.

How does curve_fit work in Python?

Curve fitting is a type of optimization that finds an optimal set of parameters for a defined function that best fits a given set of observations. Unlike supervised learning, curve fitting requires that you define the function that maps examples of inputs to outputs.

What does curve_fit return?

The curve_fit() function returns an optimal parameters and estimated covariance values as an output. Now, we'll start fitting the data by setting the target function, and x, y data into the curve_fit() function and get the output data which contains a, b, and c parameter values.

What does POPT and PCOV mean?

1. What does popt and pcov mean? popt- An array of optimal values for the parameters which minimizes the sum of squares of residuals. pcov-2d array which contains the estimated covariance of popt. The diagonals provide the variance of the parameter estimate.


1 Answers

According to doc, optimization with curve_fit gives you

Optimal values for the parameters so that the sum of the squared error of f(xdata, *popt) - ydata is minimized

Then, use optimize.leastsq

import scipy.optimize
p,cov,infodict,mesg,ier = optimize.leastsq(
        residuals,a_guess,args=(x,y),full_output=True,warning=True)

with this for residuals:

def residuals(a,x,y):
    return y-f(x,a)

residuals is the method returning difference between true output data y and model output, with f the model, a the parameter(s), x the input data.

Method optimize.leastsq is returning a lot of information you can use to compute RSquared and RMSE by yourself. For RSQuared, you can do

ssErr = (infodict['fvec']**2).sum()
ssTot = ((y-y.mean())**2).sum()
rsquared = 1-(ssErr/ssTot )

More details on what is infodict['fvec']

In [48]: optimize.leastsq?
...
      infodict -- a dictionary of optional outputs with the keys:
                  'fvec' : the function evaluated at the output
like image 135
kiriloff Avatar answered Oct 03 '22 10:10

kiriloff