Note that there are some explanatory texts on larger screens.

plurals
  1. POSlicing Sparse Matrices in Scipy -- Which Types Work Best?
    primarykey
    data
    text
    <p>The SciPy <a href="http://www.scipy.org/SciPyPackages/Sparse">Sparse Matrix tutorial</a> is very good -- but it actually leaves the section on slicing un(der)developed (still in outline form -- see section: "Handling Sparse Matrices").</p> <p>I will try and update the tutorial, once this question is answered.</p> <p>I have a large sparse matrix -- currently in dok_matrix format. </p> <pre><code>import numpy as np from scipy import sparse M = sparse.dok_matrix((10**6, 10**6)) </code></pre> <p>For various methods I want to be able to slice columns and for others I want to slice rows. Ideally I would use advanced-indexing (i.e. a boolean vector, <code>bool_vect</code>) with which to slice a sparse matrix <code>M</code> -- as in:</p> <pre><code>bool_vect = np.arange(10**6)%2 # every even index out = M[bool_vect,:] # Want to select every even row </code></pre> <p>or </p> <pre><code>out = M[:,bool_vect] # Want to select every even column </code></pre> <p>First off, dok_matrices do not support this -- but I think it works (slowly) if I first cast to lil_matrices, via <code>sparse.lil_matrix(M)</code></p> <p>As far as I can gather from the tutorial -- to slice columns I want to use CSC and to slice rows I want to slice CSR. So does that mean I should cast the matrix <code>M</code> via:</p> <pre><code>M.tocsc()[:,bool_vect] </code></pre> <p>or</p> <pre><code>M.tocsr()[bool_vect,:] </code></pre> <p>I am kinda guessing here and my code is slow because of it. Any help from someone who understands how this works would be appreciated. Thanks in advance.</p> <p>If it turns out I should not be indexing my matrix with a boolean array, but rather a list of integers (indices) -- that is also something I would be happy to find out. Whichever is more efficient.</p> <p>Finally -- this is a big matrix, so bonus points if this can happen in place / with broadcasting.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload