pseudo inverse of sparse matrix in python

后端 未结 2 1288
天涯浪人
天涯浪人 2021-02-15 17:46

I am working with data from neuroimaging and because of the large amount of data, I would like to use sparse matrices for my code (scipy.sparse.lil_matrix or csr_matrix).

<
相关标签:
2条回答
  • 2021-02-15 18:10

    You could study more on the alternatives offered in scipy.sparse.linalg.

    Anyway, please note that a pseudo-inverse of a sparse matrix is most likely to be a (very) dense one, so it's not really a fruitful avenue (in general) to follow, when solving sparse linear systems.

    You may like to describe a slight more detailed manner your particular problem (dot(A, x)= b+ e). At least specify:

    • 'typical' size of A
    • 'typical' percentage of nonzero entries in A
    • least-squares implies that norm(e) is minimized, but please indicate whether your main interest is on x_hat or on b_hat, where e= b- b_hat and b_hat= dot(A, x_hat)

    Update: If you have some idea of the rank of A (and its much smaller than number of columns), you could try total least squares method. Here is a simple implementation, where k is the number of first singular values and vectors to use (i.e. 'effective' rank).

    from scipy.sparse import hstack
    from scipy.sparse.linalg import svds
    
    def tls(A, b, k= 6):
        """A tls solution of Ax= b, for sparse A."""
        u, s, v= svds(hstack([A, b]), k)
        return v[-1, :-1]/ -v[-1, -1]
    
    0 讨论(0)
  • 2021-02-15 18:20

    Regardless of the answer to my comment, I would think you could accomplish this fairly easily using the Moore-Penrose SVD representation. Find the SVD with scipy.sparse.linalg.svds, replace Sigma by its pseudoinverse, and then multiply V*Sigma_pi*U' to find the pseudoinverse of your original matrix.

    0 讨论(0)
提交回复
热议问题