Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class method as a model function for scipy.optimize.curve_fit

There is a statement in the manual of curve_fit that

The model function, f(x, ...). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.

However, I would like to use as a model function a method of the class which is defined as:

def model_fun(self,x,par):

So, the first argument is not an independent variable, as you can see. Is there any way how I can use the method of a class as a model function for curve_fit

like image 606
freude Avatar asked May 14 '13 12:05

freude


People also ask

How does SciPy optimize curve_fit work?

The SciPy open source library provides the curve_fit() function for curve fitting via nonlinear least squares. The function takes the same input and output data as arguments, as well as the name of the mapping function to use. The mapping function must take examples of input data and some number of arguments.

What does SciPy optimize curve_fit return?

Returns poptarray. Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized. pcov2-D array. The estimated covariance of popt. The diagonals provide the variance of the parameter estimate.

What does SciPy optimize provides functions for?

SciPy optimize provides functions for minimizing (or maximizing) objective functions, possibly subject to constraints. It includes solvers for nonlinear problems (with support for both local and global optimization algorithms), linear programing, constrained and nonlinear least-squares, root finding, and curve fitting.

What is POPT and PCOV?

The return value popt contains the best-fit values of the parameters. The return value pcov contains the covariance (error) matrix for the fit parameters. From them we can determine the standard deviations of the parameters, just as we did for linear least chi square.


1 Answers

Sure, create an instance and pass its bound method:

class MyClass(object):
   ...
   def model_fun(self,x,par): ...

obj = MyClass(...)
curve_fit(obj.model_fun, ...)

You can find a good explanation about bound/unbound/etc. in this question.

like image 149
shx2 Avatar answered Sep 22 '22 13:09

shx2