numpy polyfit中使用的权重值是多少,拟合的误差是什么 [英] What are the weight values to use in numpy polyfit and what is the error of the fit
问题描述
我正在尝试对numpy中的某些数据进行线性拟合.
I'm trying to do a linear fit to some data in numpy.
Ex(其中w是该值的样本数量,即对于点(x=0, y=0)
,我只有1次测量,而该测量值是2.2
,但对于点(1,1)
,我具有3.5
的2次测量.
Ex (where w is the number of samples I have for that value, i.e. for the point (x=0, y=0)
I only have 1 measurement and the value of that measurement is 2.2
, but for the point (1,1)
I have 2 measurements with a value of 3.5
.
x = np.array([0, 1, 2, 3])
y = np.array([2.2, 3.5, 4.6, 5.2])
w = np.array([1, 2, 2, 1])
z = np.polyfit(x, y, 1, w = w)
因此,现在的问题是:
在这些情况下,在Polyfit中使用w=w
是正确的还是应该使用我应该使用的w = sqrt(w)
?
So, now the question is:
is it correct to use w=w
in polyfit for these cases or should I use w = sqrt(w)
of what should I use?
此外,如何从polyfit中获取拟合误差?
Also, how can I get the fit error from polyfit?
推荐答案
如果测量结果呈正态分布,则每个值的不确定性将与1/sqrt(n)
成比例,其中n
是测量次数.您想用不确定性的倒数来衡量您的健康状况,所以第二个猜测是最好的:w=np.sqrt(n)
If you have normally distributed measurements, then your uncertainty in each value would be proportional to 1/sqrt(n)
where n
is the number of measurements. You want to weigh your fit by the inverse of your uncertainty, so your second guess is best: w=np.sqrt(n)
要获取参数的协方差,请同时输入cov=True
.
To get the covariance on your parameters, also give cov=True
.
x = np.array([0, 1, 2, 3])
y = np.array([2.2, 3.5, 4.6, 5.2])
n = np.array([1, 2, 2, 1])
p, c = np.polyfit(x, y, 1, w=np.sqrt(n), cov=True)
cov
矩阵的对角线是每个参数的单独方差,当然,非对角线是协方差.因此,最有可能需要拟合误差"的是这些对角线的平方根:
The diagonals of your cov
matrix are the individual variances on each parameter, and of course the off-diagonals are the covariances. So most likely what you want for "fit error" is the square root of these diagonals:
e = np.sqrt(np.diag(c))
这篇关于numpy polyfit中使用的权重值是多少,拟合的误差是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!