最小二乘法拟合二维线
Least square fit a 2D line
我意识到我可以使用 numpy 来找到这样的行:
import numpy as np
import matplotlib.pyplot as plt
a = np.array([1,2,3,4,6,7])
b = np.array([5,4,3,2,-2,-1])
k,m = np.polyfit(a,b,1)
plt.scatter(a,b)
plt.plot([0,10],[m,10*k+m])
plt.show()
但我想改用原始 python 代码。我的数学太生疏了,但如果可以用几行代码完成,我将不胜感激!
如果您正在寻找基于最小化二次误差的 simple linear regression,纯 Python 实现非常简单(检查上面 link 中的 α 和 β 方程) :
def linear_fit(x, y):
"""For set of points `(xi, yi)`, return linear polynomial `f(x) = k*x + m` that
minimizes the sum of quadratic errors.
"""
meanx = sum(x) / len(x)
meany = sum(y) / len(y)
k = sum((xi-meanx)*(yi-meany) for xi,yi in zip(x,y)) / sum((xi-meanx)**2 for xi in x)
m = meany - k*meanx
return k, m
对于您的示例输入:
>>> x = [1,2,3,4,6,7]
>>> y = [5,4,3,2,-2,-1]
>>> linear_fit(x, y)
(-1.1614906832298135, 6.285714285714285)
我意识到我可以使用 numpy 来找到这样的行:
import numpy as np
import matplotlib.pyplot as plt
a = np.array([1,2,3,4,6,7])
b = np.array([5,4,3,2,-2,-1])
k,m = np.polyfit(a,b,1)
plt.scatter(a,b)
plt.plot([0,10],[m,10*k+m])
plt.show()
但我想改用原始 python 代码。我的数学太生疏了,但如果可以用几行代码完成,我将不胜感激!
如果您正在寻找基于最小化二次误差的 simple linear regression,纯 Python 实现非常简单(检查上面 link 中的 α 和 β 方程) :
def linear_fit(x, y):
"""For set of points `(xi, yi)`, return linear polynomial `f(x) = k*x + m` that
minimizes the sum of quadratic errors.
"""
meanx = sum(x) / len(x)
meany = sum(y) / len(y)
k = sum((xi-meanx)*(yi-meany) for xi,yi in zip(x,y)) / sum((xi-meanx)**2 for xi in x)
m = meany - k*meanx
return k, m
对于您的示例输入:
>>> x = [1,2,3,4,6,7]
>>> y = [5,4,3,2,-2,-1]
>>> linear_fit(x, y)
(-1.1614906832298135, 6.285714285714285)