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

前端 未结 7 1807
隐瞒了意图╮
隐瞒了意图╮ 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:09

    Scipy's curve_fit takes three positional arguments, func, xdata and ydata. So an alternative approach (to using a function wrapper) is to treat 'b' as xdata (i.e. independent variable) by building a matrix that contains both your original xdata (x1) and a second column for your fixed parameter b.

    Assuming x1 and x2 are arrays:

    def func(xdata,a):
       x, b = xdata[:,0], xdata[:,1]  # Extract your x and b
       return a*x*x + b
    
    for b in xrange(10): 
       xdata = np.zeros((len(x1),2))  # initialize a matrix
       xdata[:,0] = x1  # your original x-data
       xdata[:,1] = b  # your fixed parameter
       popt,pcov = curve_fit(func,xdata,x2)  # x2 is your y-data
    

提交回复
热议问题