1

I tried scipy.[sparse].linalg.inv. it returns an error that the matrix is not square.

I tried numpy.linalg.inv, it returns an error that it shows I passed a 0-dimensional array.

Can anyone help me how to inverse a scipy CSR matrix whose type is:

    <10000x31331 sparse matrix of type '<type 'numpy.float64'>'
    with 801667 stored elements in Compressed Sparse Row format>
Jesper - jtk.eth
  • 7,026
  • 11
  • 36
  • 63
Kyle
  • 23
  • 1
  • 6
  • 6
    You need to go learn some linear algebra. Your matrix isn't square, and only square matrices actually have an inverse. – user2357112 Nov 08 '16 at 18:58

2 Answers2

3

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 ofAis expected to be non-sparse, it will likely be faster to convertAto 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

Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353
1

Try having a look at np.linalg.pinv:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.pinv.html

This is the "generalized inverse of a matrix" which is valid for rectangular (so, non-square) matrices. Note that, as has been pointed out, there is no unique inverse to a rectangular matrix, however, if we impose additional requirements (minimize reconstruction error via least squares) we can get a unique answer. Just be careful with its interpretation.

Read some more here when you have time: https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse

Also, since you are using CSR matrices from the scipy library I assume speed matters, so read this:

The difference of pseudo-inverse between SciPy and Numpy

I am not sure if there is any method for CSR matrices similar to pinv, but if not, you could convert your CSR to a numpy matrix with the "my_csr_matrix.toarray()" method, however, consider overhead etc. (this would be application-dependent whether that is OK or not).

Community
  • 1
  • 1
Jesper - jtk.eth
  • 7,026
  • 11
  • 36
  • 63
  • I tried my_csr_matrix.toarray() and np.linalg.inv. every time I used methods of numpy. it shows LinAlgError: 0-dimensional array given. Array must be at least two-dimensional – Kyle Nov 08 '16 at 19:34
  • 1
    (1) could you please share your code and (2) notice the "p" in "pinv", it's not "inv". – Jesper - jtk.eth Nov 08 '16 at 20:50