如何计算一个向量的伪逆和行列式?(最好使用numpy或更好的pandas)
我试过这个,但是它不工作:
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:Python27libsite-packagesnumpylinalglinalg.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:Python27libsite-packagesnumpylinalglinalg.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:Python27libsite-packagesnumpylinalglinalg.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
也许你想要这个?
>>> np.linalg.pinv([[1, 2, 3, 4]])
array([[ 0.03333333],
[ 0.06666667],
[ 0.1 ],
[ 0.13333333]])
注意额外的一组括号。正如错误消息所示,您只能取矩阵的伪逆。如果你有一个向量,你需要把它变成一个1行矩阵