【发布时间】:2018-11-16 13:43:15
【问题描述】:
我是 python 新手(和一般编程),想使用curve_fit 进行多项式拟合,其中多项式的顺序(或拟合参数的数量)是可变的。
我编写了这段代码,它适用于固定数量的 3 个参数 a,b,c
# fit function
def fit_func(x, a,b,c):
p = np.polyval([a,b,c], x)
return p
# do the fitting
popt, pcov = curve_fit(fit_func, x_data, y_data)
但现在我想让我的 fit 函数仅依赖于参数数量 N 而不是 a,b,c,...。
我猜这不是一件很难的事情,但由于我的知识有限,我无法让它发挥作用。
我已经查看了this question,但无法将其应用于我的问题。
【问题讨论】:
-
我建议查看 numpy 文档docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html,页面底部有示例代码。
-
但这是
polyfit的文档。我需要使用curve_fit,因为稍后我必须为其他类型的函数扩展代码。
标签: python-3.x scipy curve-fitting