Make a small array:
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]])
Make a sparse copy:
In [439]: M=sparse.csr_matrix(A)
pinv
of the toarray
is the same thing as before:
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]])
Can't use the numpy inv directly on the sparse matrix - because it doesn't know how to properly read that data structure
In [442]: np.linalg.pinv(M)
...
LinAlgError: 0-dimensional array given. Array must be at least two-dimensional
There is a sparse linalg inv
, but it is just spsolve(A,I)
. It also warns that If the inverse of
Ais expected to be non-sparse, it will likely be faster to convert
Ato dense and use scipy.linalg.inv.
The same warning likely applies to the pinv
or equivalents.
I don't off hand see a pinv
in sparse linalg list, but it does have a lsqr
.
===================
pseudo inverse of sparse matrix in python (2011)
supports the idea that the pseudo inverse is likely to be dense. But it also suggests a sparse solution utilizing svds
.
Also
How to calculate the generalized inverse of a Sparse Matrix in scipy