scipy sparse matrix question
Robert Kern
robert.kern at gmail.com
Tue Mar 9 16:13:23 EST 2010
On 2010-03-09 14:52 PM, Victor Eijkhout wrote:
> Terry Reedy<tjreedy at 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
More information about the Python-list
mailing list