【发布时间】:2021-05-12 23:22:15
【问题描述】:
我有一个矩阵 A 和矩阵 b
A = [[536.08008658 1. ]
[580.40909091 1. ]
[624.73809524 1. ]
[671.83766234 1. ]
[718.93722944 1. ]
[760.495671 1. ]
[810.36580087 1. ]
[857.46536797 1. ]]
b = [[210.82467532]
[188.66017316]
[172.03679654]
[166.495671 ]
[169.26623377]
[177.57792208]
[196.97186147]
[224.67748918]]
如何找到 2 x 1 矩阵的矩阵 x?
我尝试了lsq = np.linalg.solve(A, b),但出现了这个错误:
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-18-e0f5641243e6> in <module>
17
18 # a, b = np.linalg.lstsq(A, y, rcond=None)[0]
---> 19 lsq = np.linalg.solve(A, y)
20 print(lsq)
21
<__array_function__ internals> in solve(*args, **kwargs)
~/opt/anaconda3/lib/python3.8/site-packages/numpy/linalg/linalg.py in solve(a, b)
379 a, _ = _makearray(a)
380 _assert_stacked_2d(a)
--> 381 _assert_stacked_square(a)
382 b, wrap = _makearray(b)
383 t, result_t = _commonType(a, b)
~/opt/anaconda3/lib/python3.8/site-packages/numpy/linalg/linalg.py in _assert_stacked_square(*arrays)
202 m, n = a.shape[-2:]
203 if m != n:
--> 204 raise LinAlgError('Last 2 dimensions of the array must be square')
205
206 def _assert_finite(*arrays):
LinAlgError: Last 2 dimensions of the array must be square
任何关于如何使用该特定方法解决此问题的想法都会有所帮助。谢谢。
【问题讨论】:
-
你想在这里做什么?找到一个矩阵
x做什么?您的问题没有明确说明。 -
我正在尝试获取 x 中的未知数 a 和 b 以求解 y = ax + b 的最小二乘估计量。
-
numpy.linalg.solve不是最小二乘估计器。这是工作的错误工具。numpy.linalg.lstsq是最小二乘估计器。
标签: python numpy linear-algebra least-squares