[SciPy-user] numpy.compress and sparse matrices?

Nathan Bell wnbell@gmail....
Tue Jun 9 22:04:45 CDT 2009


On Tue, Jun 9, 2009 at 10:11 AM, Marco Lui<saffsd@gmail.com> wrote:
> Hello everyone.
>
> I am looking for a sparse matrix implementation that supports the
> numpy.compress operation, which is akin to "fancy indexing" by a boolean
> vector. Working in scipy 0.6.0, I have not been able to find such an
> implementation. I usually get an error like the following:
>
> The above example works correctly when data is a normal numpy array. Does
> any implementation of sparse arrays compatible with numpy.compress exist?
>

The sparse csr_matrix and csc_matrix support fancy row and column
indexing.  You could just use train_indices.nonzero() to convert the
boolean array to an index array and pass that into the normal []
operator as follows:
  data[train_indices.nonzero()[0], :]

(untested)

FWIW, CSR should be faster for row extraction and CSC should be faster
for columns.

-- 
Nathan Bell wnbell@gmail.com
http://www.wnbell.com/


More information about the SciPy-user mailing list