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

前端 未结 7 1792
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 07:55

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 o

7条回答
  •  無奈伤痛
    2020-12-01 08:10

    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 Model
    def func(x,a,b):
       return a*x*x + b
    
    # create model
    fmodel = Model(func)
    # create parameters -- these are named from the function arguments --
    # giving initial values
    params = 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))
    

提交回复
热议问题