[SciPy-user] accessing only the nonzero elements of a sparse matrix

Ted Sandler ted.sandler@gmail....
Sat Jun 28 21:18:00 CDT 2008


It doesn't seem like anything like the numpy arrays method ``nonzero''
is implemented for scipy's sparse matrices.  What I am looking for is
something like:

  A = sparse.spidentity(3);
  A.nonzero()
  ==>
    (array([0, 1, 2]), array([0, 1, 2]))

I guess I could convert to coordinate form 'COO' and then access the
"row" and "col" fields but this seems a little evil as it relies on
properties of the 'COO' matrix which could change in the future.  It
would be nicer to have a uniform method of all sparse matrix types.

Thanks for any info on this,
-Ted


More information about the SciPy-user mailing list