如何计算多项式拟合的误差(斜率和截距) [英] How to calculate error for polynomial fitting (in slope and intercept)
本文介绍了如何计算多项式拟合的误差(斜率和截距)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算scipy.polyfit函数计算的斜率和截距误差.我对ydata有(+/-)不确定性,那么如何将其包括在内以将不确定性计算为斜率和截距?我的代码是
Hi I want to calculate errors in slope and intercept which are calculated by scipy.polyfit function. I have (+/-) uncertainty for ydata so how can I include it for calculating uncertainty into slope and intercept? My code is,
from scipy import polyfit
import pylab as plt
from numpy import *
data = loadtxt("data.txt")
xdata,ydata = data[:,0],data[:,1]
x_d,y_d = log10(xdata),log10(ydata)
polycoef = polyfit(x_d, y_d, 1)
yfit = 10**( polycoef[0]*x_d+polycoef[1] )
plt.subplot(111)
plt.loglog(xdata,ydata,'.k',xdata,yfit,'-r')
plt.show()
非常感谢
推荐答案
You could use scipy.optimize.curve_fit
instead of polyfit
. It has a parameter sigma
for errors of ydata. If you have your error for every y value in a sequence yerror
(so that yerror
has the same length as your y_d
sequence) you can do:
polycoef, _ = scipy.optimize.curve_fit(lambda x, a, b: a*x+b, x_d, y_d, sigma=yerror)
For an alternative see the paragraph Fitting a power-law to data with errors in the Scipy Cookbook.
这篇关于如何计算多项式拟合的误差(斜率和截距)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文