scipy sparse matrix question

  • Thread starter Victor Eijkhout
  • Start date
V

Victor Eijkhout

I can't find any detailed information about scipy.sparse.

My specific question: what does "for x in A" give me when A is a sparse
matrix? It seems to yield all nonzero locations, but in what kind of
form? Very specifically: how do I get the (i,j) coordinates and the
value from x?

Victor.
 
R

Robert Kern

I can't find any detailed information about scipy.sparse.

The place to ask would be on scipy-user:

http://www.scipy.org/Mailing_Lists
My specific question: what does "for x in A" give me when A is a sparse
matrix? It seems to yield all nonzero locations,

No, it gives you rows, just like a dense 2D array.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
T

Terry Reedy

I can't find any detailed information about scipy.sparse.

Scipy questions are best asked on the scipy list, which I suspect you
can also access via news.gmane.org.
My specific question: what does "for x in A" give me when A is a sparse
matrix?

Try it and see what you get.
 
V

Victor Eijkhout

Terry Reedy said:
Try it and see what you get.

Ah, how do I see what I get? If I print it it looks plausible, but I
don't know how to pull it apart. It doesn't seem to be an array.

Victor.
 
R

Robert Kern

Ah, how do I see what I get? If I print it it looks plausible, but I
don't know how to pull it apart. It doesn't seem to be an array.

In [5]: I = sparse.csr_matrix(np.eye(4))

In [6]: list(I)
Out[6]:
[<1x4 sparse matrix of type '<type 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>,
<1x4 sparse matrix of type '<type 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>,
<1x4 sparse matrix of type '<type 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>,
<1x4 sparse matrix of type '<type 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>]

What is unclear about that? They are obviously 1x4 sparse matrices, i.e. the
rows of the matrix. Use the .indices and .data attributes to get the indices of
the nonzero values and the corresponding values.

Details may differ depending on the format of sparse matrix. Some aren't even
iterable.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top