如何将多项式拟合到带有误差线的数据
我目前正在使用 numpy.polyfit(x,y,deg) 将多项式拟合到实验数据。然而,我想拟合一个基于点误差使用加权的多项式。
我发现 scipy.curve_fit 它利用了权重,我想我可以将函数“f”设置为我想要的阶数的多项式,然后将权重放入'sigma',这应该可以实现我的目标。
我想知道还有另一种更好的方法吗?
非常感谢。
I am currently using numpy.polyfit(x,y,deg) to fit a polynomial to experimental data. I would however like to fit a polynomial that uses weighting based on the errors of the points.
I have found scipy.curve_fit which makes use of weights and I suppose I could just set the function, 'f', to the form a polynomial of my desired order, and put my weights in 'sigma', which should achieve my goal.
I was wondering is there another, better way of doing this?
Many Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的做法,有很多评论!
注意:我使用 q 阶和 n 阶多项式拟合来完成此操作。
Here is how I did it, with lots of comments!
Note: I did it with qth and nth order polynomial fits.
对于加权多项式拟合,您可以使用:
请参阅http: //docs.scipy.org/doc/numpy/reference/ generated/numpy.polynomial.polynomial.polyfit.html
重要的是要注意,在此函数中,权重应该不以
1/variance
形式提供(这是许多加权应用中的常见形式),而是以1/sigma
形式提供 虽然curve_fit 和
leastsq
是比polyfit
更通用、更强大的优化工具(因为它们可以适应任何 函数),polyfit
有优点是它能产生(精确的)解析解,因此可能比 curve_fit 和 leastsq 等迭代近似方法快得多 - 特别是在将多项式拟合到多个集合的情况下y 数据(在同一 x 向量处获得)更新: 从 numpy 版本 1.7 开始,
numpy.polyfit
也采用权重作为输入(理想情况下应以1/sigma
形式提供,而不是1/variance
)For weighted polynomial fitting you can use:
see http://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.polyfit.html
Important to note that in this function the weights should not be supplied as
1/variance
(which is the usual form in many weighted applications), but as1/sigma
Although
curve_fit
andleastsq
are much more general and powerful optimization tools thanpolyfit
(in that they can fit just any function),polyfit
has the advantage that it yields an (exact) analytical solution and is therefore probably much faster than iterative approximation methods likecurve_fit
andleastsq
- especially in the case of fitting polynomials to multiple sets of y-data (obtained at the same x-vector)Update: As of numpy version 1.7,
numpy.polyfit
also takes weights as an input (which ideally should be supplied as1/sigma
, not1/variance
)看看http://scipy-cookbook.readthedocs.io/items/FittingData。 html 特别是 '拟合幂律到有错误的数据'。它展示了如何将 scipy.optimize.leastsq 与包含误差加权的函数一起使用。
Take a look at http://scipy-cookbook.readthedocs.io/items/FittingData.html in particular the section 'Fitting a power-law to data with errors'. It shows how to use scipy.optimize.leastsq with a function that includes error weighting.