Pass tuple as input argument for scipy.optimize.curve_fit

后端 未结 4 1707
刺人心
刺人心 2021-02-14 20:21

I have the following code:

import numpy as np
from scipy.optimize import curve_fit


def func(x, p): return p[0] + p[1] + x


popt, pcov = curve_fit(func, np.ara         


        
4条回答
  •  情歌与酒
    2021-02-14 20:38

    Not sure if this is cleaner, but at least it is easier now to add more parameters to the fitting function. Maybe one could even make an even better solution out of this.

    import numpy as np
    from scipy.optimize import curve_fit
    
    
    def func(x, p): return p[0] + p[1] * x
    
    def func2(*args):
        return func(args[0],args[1:])
    
    popt, pcov = curve_fit(func2, np.arange(10), np.arange(10), p0=(0, 0))
    print popt,pcov
    

    EDIT: This works for me

    import numpy as np
    from scipy.optimize import curve_fit
    
    def func(x, *p): return p[0] + p[1] * x
    
    popt, pcov = curve_fit(func, np.arange(10), np.arange(10), p0=(0, 0))
    print popt,pcov
    

提交回复
热议问题