Fitting only one parameter of a function with many parameters in python Fitting only one parameter of a function with many parameters in python python python

Fitting only one parameter of a function with many parameters in python


You can wrap func in a lambda, as follows:

def func(x,a,b):   return a*x*x + bfor 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.


A better approach would use lmfit, which provides a higher level interface to curve-fitting. Among other features, Lmfit makes fitting parameters be first-class objects that can have bounds or be explicitly fixed (among other features).

Using lmfit, this problem might be solved as:

from lmfit import Modeldef func(x,a,b):   return a*x*x + b# create modelfmodel = Model(func)# create parameters -- these are named from the function arguments --# giving initial valuesparams = fmodel.make_params(a=1, b=0)# fix b:params['b'].vary = False# fit parameters to data with various *static* values of b:for b in range(10):   params['b'].value = b   result = fmodel.fit(ydata, params, x=x)   print(": b=%f, a=%f+/-%f, chi-square=%f" % (b, result.params['a'].value,                                              result.params['a'].stderr,                                             result.chisqr))


Instead of using the lambda function which might be less intuitive to digest I would recommend to specify the scikit curve_fit parameter bounds that will force your parameter to be searched within custom boundaries.

All you have to do is to let your variable a move between -inf and +inf and your variable b between (b - epsilon) and (b + epsilon)

In your example:

epsilon = 0.00001def func(x,a,b):    return a*x*x + bfor b in xrange(10):    popt,pcov = curve_fit(func,x1,x2, bounds=((-np.inf,b-epsilon), (np.inf,b+epsilon))