Using scipy curve_fit for a variable number of parameters

后端 未结 2 1869
野性不改
野性不改 2020-12-02 02:16

I have a fitting function which has the form:

def fit_func(x_data, a, b, c, N)

where a, b, c are lists of lenth N, every entry of which is

相关标签:
2条回答
  • 2020-12-02 03:04

    I was able to solve the same problem a little bit differently. I used scip.optimize.least_squares for solving rather than curv_fit. I have discussed my solution under the link- https://stackoverflow.com/a/60409667/11253983

    0 讨论(0)
  • 2020-12-02 03:15

    The solution here is to write a wrapper function that takes your argument list and translates it to variables that the fit function understands. This is really only necessary since I am working qwith someone else's code, in a more direct application this would work without the wrapper layer. Basically

    def wrapper_fit_func(x, N, *args):
        a, b, c = list(args[0][:N]), list(args[0][N:2*N]), list(args[0][2*N:3*N])
        return fit_func(x, a, b, c, N)
    

    and to fix N you have to call it in curve_fit like this:

    popt, pcov = curve_fit(lambda x, *params_0: wrapper_fit_func(x, N, params_0), x, y, p0=params_0)
    

    where

    params_0 = [a_1, ..., a_N, b_1, ..., b_N, c_1, ..., c_N]
    
    0 讨论(0)
提交回复
热议问题