In python I have a function which has many parameters. I want to fit this function to a data set, but using only one parameter, the rest of the parameters I want to supply on on my own. Here is an example:
def func(x,a,b): return a*x*x + b for b in xrange(10): popt,pcov = curve_fit(func,x1,x2)
In this I want that the fitting is done only for a
and the parameter b
takes the value of the loop variable. How can this be done?
In Python 3.7 and newer, there is no limit.
Parametric fitting involves finding coefficients (parameters) for one or more models that you fit to data. The data is assumed to be statistical in nature and is divided into two components: data = deterministic component + random component.
It doesn't matter what order they are given in the parameter so long as the arguments in the call expression match the correct variable. Either way won't matter to the output string.
You can wrap func
in a lambda, as follows:
def func(x,a,b): return a*x*x + b for b in xrange(10): popt,pcov = curve_fit(lambda x, a: func(x, a, b), x1, x2)
A lambda is an anonymous function, which in Python can only be used for simple one line functions. Basically, it's normally used to reduce the amount of code when don't need to assign a name to the function. A more detailed description is given in the official documentation: http://docs.python.org/tutorial/controlflow.html#lambda-forms
In this case, a lambda is used to fix one of the arguments of func
. The newly created function accepts only two arguments: x
and a
, whereas b
is fixed to the value taken from the local b
variable. This new function is then passed into curve_fit
as an argument.
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