python - 理解 numpy 的 lstsq

标签 python numpy least-squares

我理解最小二乘解之和的想法。解的参数反射(reflect)了使平方误差最小化的系数。但我无法理解 lstsq函数可从 numpy.linalg 获得。例如,我尝试了以下方法:

m1 = np.asarray([[1,2],[3,4],[5,6],[7,8]])

m2 = np.asarray([[9,10],[11,12],[13,14],[15,16]])


solution = np.linalg.lstsq(m1, m2)[0]

解决方案的值为:

array([[-7., -8.],
   [ 8.,  9.]])

这个输出是什么意思?我无法想象/理解这个结果。

最佳答案

我会孤注一掷。该方法返回方程 y=mx+cmc。当您为 b 参数传递一个二维数组时,您会得到两个拟合 - 一个用于第一列,一个用于第二列;就像您要求两个不同数据集/向量的拟合一样。

In [22]: sol
Out[22]: 
array([[-7., -8.],
       [ 8.,  9.]])

In [23]: sol[:,0], sol[:,1]
Out[23]: (array([-7.,  8.]), array([-8.,  9.]))

In [24]: np.linalg.lstsq(m1,m2[:,0])[0]
Out[24]: array([-7.,  8.])

In [25]: np.linalg.lstsq(m1,m2[:,1])[0]
Out[25]: array([-8.,  9.])


In [30]: np.linalg.lstsq(m1, np.array([9,11,13,15]))[0]
Out[30]: array([-7.,  8.])

In [31]: np.linalg.lstsq(m1, np.array([10,12,14,16]))[0]
Out[31]: array([-8.,  9.])

关于python - 理解 numpy 的 lstsq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54753132/

相关文章:

python - 使用 sklearn 在 Python 中拟合的高斯混合模型太慢 - 还有其他选择吗?

python - 相机捕获蓝色时如何打印文本?

python - 如何找到距其他三个点特定距离的点的坐标?

scipy - SciPy:Minimumsq与Minimum_squares

python - 如何设置 curve_fit 的初始值以找到最佳优化,而不仅仅是局部优化?

python - 将 `defaultdict` 公开为常规 `dict`

python - 将 Pygame 模拟保存为 mp4 文件

python - 如何在 Pandas DataFrame where 子句中使用特定列的值?

math - 我如何在 MATLAB 中使用最小二乘近似?

python - pip uninstall 工作但给出错误