Search code examples
pythonsparse-matrix

How to get a row from coo_matrix as a dense vector in Python?


I'm new to Python and could you help me about some basic sparse matrix operation:

  1. How to extract a dense row vector from a sparse matrix without make the whole matrix dense beforehand? coo_matrix.getrow() only returns a sparse representation

  2. How to extract a proportion of rows (say, 80%) randomly from a sparse matrix? I need to use them as training data and the proportion left as test data.

Thanks in advance!


Solution

    1. coo_matrix.getrow().todense()
    2. Use a different sparse representation that supports slicing, for example csr_matrix. For sparse matrix A, A[i] will give the ith row.

    For example:

    In [9]: from random import sample
    
    In [10]: A = csr_matrix(...)
    
    In [11]: n = A.shape[0]
    
    In [12]: indices = sample(range(n), 4*n/5)
    
    In [13]: A[indices].todense()