python - numpy.polyfit 给出空残差数组

标签 python arrays python-2.7 numpy curve-fitting

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

fit1, fit_err1, _, _, _ = np.polyfit(xint[:index_max], yint[:index_max], 2、 全=真)

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

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

编辑: 一个示例数据集:

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

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

结果:

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

我知道将二阶多项式拟合到一组三点不是很有用,但我仍然希望该函数发出警告,或者(因为它实际上确定了拟合)返回实际残差,或者两者(比如“这是残差,但你的条件很差!”)。

最佳答案

正如@Jaime 所指出的,如果您有三个点,则二阶多项式将完全适合它。你认为错误应该是 0 而不是空数组的观点是有道理的,但这是 np.linalg.lstsq 的当前行为,which is where np.polyfit is wrapped around .

我们可以通过最小二乘法拟合 y = 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]))

我们可以看到第二个输出是一个空数组。和 this is intended to work like this .

关于python - numpy.polyfit 给出空残差数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26429409/

相关文章:

Python - 作业 - 将任意基数转换为任意基数

python - 无法打开 C :\Python27\Scripts\pip-scripts. py

python - python set 和 dict "internally"的区别

python - 具有最小值、最大值、平均值和标准差的箱线图

python - 更快的字节数组列表理解/转换?

c++ - 如何通过常数优化数组乘法?

php - 如何在 PHP 中更改数组的键?

python - 如何将一个热图堆叠在另一个热图之上

Python 等同于 Data::Dumper - 转储原始数据结构以供检查?

C++ 数组作为函数参数