做一个小数组:
In [435]: A=np.array([[1,0,2,0],[0,1,3,0],[3,0,0,4]])
In [436]: A
Out[436]:
array([[1, 0, 2, 0],
[0, 1, 3, 0],
[3, 0, 0, 4]])
In [437]: np.linalg.pinv(A)
Out[437]:
array([[ 0.61538462, -0.36923077, 0.04615385],
[-0.57692308, 0.44615385, 0.06923077],
[ 0.19230769, 0.18461538, -0.02307692],
[-0.46153846, 0.27692308, 0.21538462]])
制作稀疏副本:
In [439]: M=sparse.csr_matrix(A)
toarray 的pinv 和之前是一样的:
In [441]: np.linalg.pinv(M.toarray())
Out[441]:
array([[ 0.61538462, -0.36923077, 0.04615385],
[-0.57692308, 0.44615385, 0.06923077],
[ 0.19230769, 0.18461538, -0.02307692],
[-0.46153846, 0.27692308, 0.21538462]])
不能直接在稀疏矩阵上使用 numpy inv - 因为它不知道如何正确读取该数据结构
In [442]: np.linalg.pinv(M)
...
LinAlgError: 0-dimensional array given. Array must be at least two-dimensional
有一个稀疏的 linalg inv,但它只是 spsolve(A,I)。它还警告If the inverse ofAis expected to be non-sparse, it will likely be faster to convertAto dense and use scipy.linalg.inv. 同样的警告可能适用于pinv 或等效项。
我没有在稀疏的线性列表中看到pinv,但它确实有一个lsqr。
====================
pseudo inverse of sparse matrix in python (2011)
支持伪逆很可能是稠密的观点。但它也建议使用svds 的稀疏解决方案。
还有
How to calculate the generalized inverse of a Sparse Matrix in scipy