计算最小二乘时系数矩阵中的那些
Ones in the coefficient matrix when calculating least squares
函数numpy.linalg.lstsq
可用于计算一些点x和y的近似函数。在 Numpy documentation 上给出了一个例子:
>>> x = np.array([0, 1, 2, 3])
>>> y = np.array([-1, 0.2, 0.9, 2.1])
>>> A = np.vstack([x, np.ones(len(x))]).T
>>> A
array([[ 0., 1.],
[ 1., 1.],
[ 2., 1.],
[ 3., 1.]])
>>> m, c = np.linalg.lstsq(A, y, rcond=None)[0]
问题是,为什么我们必须创建矩阵 A
然后在每个 x
参数后添加一个?为什么不能只传递数组 x
?
We can rewrite the line equation as y = Ap, where A = [[x 1]] and p = [[m], [c]].
Taken from the document you linked
这只是另一种写法
y = m*x + c*1 = m*x + c
这是行向量和列向量的点积。关于导致这种情况的设计决策,我无法给你一个理由。不过,我认为这是一件好事,因为例如力扭矩传感器校准程序要求您在没有偏移的情况下适应您测量的内容(如:c = const. = 0)。通过替换
A = np.vstack([x, np.ones(len(x))]).T
和
A = np.vstack([x, np.zeros(len(x))]).T
您将能够强制拟合通过原点。对于某些工程方面来说很整洁,尤其是对于组件的鉴定。
函数numpy.linalg.lstsq
可用于计算一些点x和y的近似函数。在 Numpy documentation 上给出了一个例子:
>>> x = np.array([0, 1, 2, 3])
>>> y = np.array([-1, 0.2, 0.9, 2.1])
>>> A = np.vstack([x, np.ones(len(x))]).T
>>> A
array([[ 0., 1.],
[ 1., 1.],
[ 2., 1.],
[ 3., 1.]])
>>> m, c = np.linalg.lstsq(A, y, rcond=None)[0]
问题是,为什么我们必须创建矩阵 A
然后在每个 x
参数后添加一个?为什么不能只传递数组 x
?
We can rewrite the line equation as y = Ap, where A = [[x 1]] and p = [[m], [c]].
Taken from the document you linked
这只是另一种写法
y = m*x + c*1 = m*x + c
这是行向量和列向量的点积。关于导致这种情况的设计决策,我无法给你一个理由。不过,我认为这是一件好事,因为例如力扭矩传感器校准程序要求您在没有偏移的情况下适应您测量的内容(如:c = const. = 0)。通过替换
A = np.vstack([x, np.ones(len(x))]).T
和
A = np.vstack([x, np.zeros(len(x))]).T
您将能够强制拟合通过原点。对于某些工程方面来说很整洁,尤其是对于组件的鉴定。