[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

scipy sparse matrix question

see

3/9/2010 6:15:00 AM

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.

--
Victor Eijkhout -- eijkhout at tacc utexas edu
4 Answers

Robert Kern

3/9/2010 4:01:00 PM

0

On 2010-03-09 00:14 AM, Victor Eijkhout wrote:
> I can't find any detailed information about scipy.sparse.

The place to ask would be on scipy-user:

http://www.scipy.org/Mai...

> 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

Terry Reedy

3/9/2010 5:14:00 PM

0

On 3/9/2010 1:14 AM, Victor Eijkhout wrote:
> 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.

see

3/9/2010 8:52:00 PM

0

Terry Reedy <tjreedy@udel.edu> wrote:

> > My specific question: what does "for x in A" give me when A is a sparse
> > matrix?
>
> 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.

--
Victor Eijkhout -- eijkhout at tacc utexas edu

Robert Kern

3/9/2010 9:13:00 PM

0

On 2010-03-09 14:52 PM, Victor Eijkhout wrote:
> Terry Reedy<tjreedy@udel.edu> wrote:
>
>>> My specific question: what does "for x in A" give me when A is a sparse
>>> matrix?
>>
>> 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.

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