numpy.polyfit给出空残差数组

时间:2022-04-19 01:44:04

I use numpy.polyfit to fit a 2nd order polynom to a set of data

我使用numpy.polyfit将二阶多项式拟合到一组数据

fit1, fit_err1, _, _, _ = np.polyfit(xint[:index_max], yint[:index_max], 2, full=True)

fit1,fit_err1,_,_,_ = np.polyfit(xint [:index_max],yint [:index_max],2,full = True)

For some few examples of my data, the variable fit_err1 is empty although the fit was successful, i.e. fit1 is not empty!

对于我的数据的一些例子,变量fit_err1是空的虽然拟合成功,即fit1不为空!

Does anybody know what an empty residual means in this context? Thank you!

有人知道在这种情况下空残余是什么意思吗?谢谢!

EDIT: one example data set:

编辑:一个示例数据集:

x = [-488., -478., -473.]
y = [ 0.02080881,  0.03233648,  0.03584448]

fit1, fit_err1, _, _, _ = np.polyfit(x, y, 2, full=True)

result:

fit1 = [ -3.00778818e-05  -2.79024663e-02  -6.43272769e+00]
fit_err1 = []

I know that fitting a 2nd order polynom to a set of three point is not very useful, but then i still expect the function to either raise a warning, or (as it actually determined a fit) return the actual residuals, or both (like "here are the residuals, but your conditions are poor!").

我知道将二阶多项式拟合到三个点的集合并不是很有用,但是我仍然期望函数要么提出警告,要么(因为它实际上确定了拟合)返回实际残差,或两者(如“这是剩余的,但你的条件很差!”)。

1 个解决方案

#1


5  

As pointed out by @Jaime, if you have three points a second order polynomial will fit it exactly. And your point that the error should be rather 0 than an empty array makes sense, but this is the current behavior of np.linalg.lstsq, which is where np.polyfit is wrapped around.

正如@Jaime所指出的,如果你有三个点,那么二阶多项式将完全适合它。并且你的观点是错误应该比空数组更有意义,但这是np.linalg.lstsq的当前行为,这是np.polyfit被包裹的地方。

We can test this behavior doing the least-squares fit of a y = a*x**0 + b*x**1 + c*x**2 equation that we know the answer should be a=0, b=0, c=1:

我们可以测试这种行为做ay = a * x ** 0 + b * x ** 1 + c * x ** 2方程的最小二乘拟合,我们知道答案应该是a = 0,b = 0, C = 1:

np.linalg.lstsq([[1, 1 ,1], [1, 2, 4], [1, 3, 9]], [1, 4, 9])
#(array([ -3.43396424e-15,   3.88578059e-15,   1.00000000e+00]),
# array([], dtype=float64),
# 3,
# array([ 10.64956309,   1.2507034 ,   0.15015641]))

where we can see that the second output is an empty array. And this is intended to work like this.

我们可以看到第二个输出是一个空数组。这是为了这样工作。

#1


5  

As pointed out by @Jaime, if you have three points a second order polynomial will fit it exactly. And your point that the error should be rather 0 than an empty array makes sense, but this is the current behavior of np.linalg.lstsq, which is where np.polyfit is wrapped around.

正如@Jaime所指出的,如果你有三个点,那么二阶多项式将完全适合它。并且你的观点是错误应该比空数组更有意义,但这是np.linalg.lstsq的当前行为,这是np.polyfit被包裹的地方。

We can test this behavior doing the least-squares fit of a y = a*x**0 + b*x**1 + c*x**2 equation that we know the answer should be a=0, b=0, c=1:

我们可以测试这种行为做ay = a * x ** 0 + b * x ** 1 + c * x ** 2方程的最小二乘拟合,我们知道答案应该是a = 0,b = 0, C = 1:

np.linalg.lstsq([[1, 1 ,1], [1, 2, 4], [1, 3, 9]], [1, 4, 9])
#(array([ -3.43396424e-15,   3.88578059e-15,   1.00000000e+00]),
# array([], dtype=float64),
# 3,
# array([ 10.64956309,   1.2507034 ,   0.15015641]))

where we can see that the second output is an empty array. And this is intended to work like this.

我们可以看到第二个输出是一个空数组。这是为了这样工作。