如何计算向量的伪逆和行列式? (最好有numpy或更好的熊猫)
我尝试了这个,但它不起作用:
import numpy
vect = [1, 2, 3, 4]
numpy.linalg.pinv(vect)
但是我收到了这个错误:
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-106-e362654e383f> in <module>()
19 vect = [1, 2, 3, 4]
---> 20 print(np.linalg.pinv(vect))
C:\Python27\lib\site-packages\numpy\linalg\linalg.pyc in pinv(a, rcond)
1544 _assertNonEmpty(a)
1545 a = a.conjugate()
-> 1546 u, s, vt = svd(a, 0)
1547 m = u.shape[0]
1548 n = vt.shape[1]
C:\Python27\lib\site-packages\numpy\linalg\linalg.pyc in svd(a, full_matrices, compute_uv)
1269 """
1270 a, wrap = _makearray(a)
-> 1271 _assertRank2(a)
1272 _assertNonEmpty(a)
1273 m, n = a.shape
C:\Python27\lib\site-packages\numpy\linalg\linalg.pyc in _assertRank2(*arrays)
153 if len(a.shape) != 2:
154 raise LinAlgError, '%d-dimensional array given. Array must be \
--> 155 two-dimensional' % len(a.shape)
156
157 def _assertSquareness(*arrays):
LinAlgError: 1-dimensional array given. Array must be two-dimensional
答案 0 :(得分:4)
也许你想要这个?
>>> np.linalg.pinv([[1, 2, 3, 4]])
array([[ 0.03333333],
[ 0.06666667],
[ 0.1 ],
[ 0.13333333]])
请注意额外的括号。正如错误消息所示,您只能采用矩阵的伪逆。如果你只有一个向量,你需要将它变成一个行矩阵。