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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With