在使用curve_fit的时候,如果有多个自变量,只需要使用tuple作为输入即可,例如:

# Fit function, deps = x[0], taum=x[1]
def expression_to_fit(X, a, b, c):
    dep, tau = X
    return a * (1 - np.exp(-tau / b)) * (1 - np.exp(-tau * dep * c))

在进行拟合时:

# fit the function, initial guess a=2e-4, b=50, c=10
popt, pcov = curve_fit(
    expression_to_fit, (xx_values[0, :], xx_values[1, :]), yy_values, p0=fit_init_guess
)

注意输入的X值必须是数列的tuple,不能是tuple的数列,我也不知道为什么。

另外很多时候你拟合失败大概率是初值没选好,有的时候会有些奇怪的提示。

最后修改:2022 年 07 月 04 日
如果觉得我的文章对你有用,请随意赞赏