如何从一维 Python 的 polyfit 中获取最小 squares/error 的总和

How to get the sum of least squares/error from polyfit in one dimension Python

我想用polyfit做一个散点图的线性回归,也想做残差,看看线性回归好不好。但是我不确定我是如何得到这个的,因为它是一维的,所以不可能从 polyfit 中得到残差作为输出值。我的代码:

p = np.polyfit(lengths, breadths, 1)
m = p[0]
b = p[1]
yfit = np.polyval(p,lengths)
newlengths = []
for y in lengths:
    newlengths.append(y*m+b)
ax.plot(lengths, newlengths, '-', color="#2c3e50")

我在他们使用 polyval 的地方看到了一个 Whosebug 的答案 - 但我不确定那给了我什么。这是长度的确切值吗?我应该通过从 polyval 和 'breadth' 中找到每个元素的增量来找到错误吗?

您可以在调用 polyfit(参见 http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html)时使用关键字 full=True 来获得适合您的最小二乘误差:

coefs, residual, _, _, _ = np.polyfit(lengths, breadths, 1, full=True)

您可以通过以下方式获得相同的答案:

coefs = np.polyfit(lengths, breadths, 1)
yfit = np.polyval(coefs,lengths)
residual = np.sum((breadths-yfit)**2)

residual = np.std(breadths-yfit)**2 * len(breadths)

另外,如果你想绘制残差,你可以这样做:

coefs = np.polyfit(lengths, breadths, 1)
yfit = np.polyval(coefs,lengths)
plot(lengths, breadths-yfit)