如何将多项式拟合到带有误差线的数据

发布于 2024-11-19 12:27:23 字数 339 浏览 3 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音盲 2024-11-26 12:27:24

这是我的做法,有很多评论!

注意:我使用 q 阶 n 阶多项式拟合来完成此操作。

from numpy import *
import pylab

# get data
fn = 'cooltemp.dat'
x, y, xerr, yerr = loadtxt(fn,unpack=True, usecols=[0,1,2,3])

# create nth degree polynomial fit
n = 1
zn = polyfit(x,y,n) 
pn = poly1d(zn) # construct polynomial 

# create qth degree polynomial fit
q = 5
zq = polyfit(x,y,q) 
pq = poly1d(zq)

# plot data and fit
xx = linspace(0, max(x), 500)
pylab.plot(xx, pn(xx),'-g', xx, pq(xx),'-b')
pylab.errorbar(x, y, xerr, yerr, fmt='r.')

# customise graph
pylab.legend(['degree '+str(n),'degree '+str(q),'data'])
pylab.axis([0,max(x),0,max(y)])
pylab.xlabel('x label (unit)')
pylab.ylabel('y label (unit)')

pylab.show()

Here is how I did it, with lots of comments!

Note: I did it with qth and nth order polynomial fits.

from numpy import *
import pylab

# get data
fn = 'cooltemp.dat'
x, y, xerr, yerr = loadtxt(fn,unpack=True, usecols=[0,1,2,3])

# create nth degree polynomial fit
n = 1
zn = polyfit(x,y,n) 
pn = poly1d(zn) # construct polynomial 

# create qth degree polynomial fit
q = 5
zq = polyfit(x,y,q) 
pq = poly1d(zq)

# plot data and fit
xx = linspace(0, max(x), 500)
pylab.plot(xx, pn(xx),'-g', xx, pq(xx),'-b')
pylab.errorbar(x, y, xerr, yerr, fmt='r.')

# customise graph
pylab.legend(['degree '+str(n),'degree '+str(q),'data'])
pylab.axis([0,max(x),0,max(y)])
pylab.xlabel('x label (unit)')
pylab.ylabel('y label (unit)')

pylab.show()
嘿嘿嘿 2024-11-26 12:27:23

对于加权多项式拟合,您可以使用:

numpy.polynomial.polynomial.polyfit(x, y, deg, rcond=None, full=False, w=weights)

请参阅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:

numpy.polynomial.polynomial.polyfit(x, y, deg, rcond=None, full=False, w=weights)

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 as 1/sigma

Although curve_fit and leastsq are much more general and powerful optimization tools than polyfit (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 like curve_fit and leastsq - 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 as 1/sigma, not 1/variance)

温柔一刀 2024-11-26 12:27:23

看看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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文