我有矩阵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]]
如何找到矩阵x,它是一个2x1矩阵
我尝试了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
任何关于如何用这种特定方法解决这一问题的想法都会有所帮助。谢谢
您为该作业调用了错误的函数。这就是求解一个正常的线性系统,它需要与变量数量相同的方程(即,它必须是平方的)。你要找的是^{} ,它返回线性矩阵方程的最小二乘解
如果您必须使用np.linalg.solve,那么您可以按如下操作。但是,使用专门的最小二乘法程序会更好(更快、更准确)
(对不起,这不是python,但我不懂python) 这是“正常方程”方法
相关问题 更多 >
编程相关推荐