[SciPy-dev] feedback on scipy.sparse

Robert Cimrman cimrman3@ntc.zcu...
Thu Dec 13 05:01:45 CST 2007

```Matthieu Brucher wrote:
> 2007/12/13, Robert Cimrman <cimrman3@ntc.zcu.cz>:
>> Hi Matthieu,
>>
>> Matthieu Brucher wrote:
>>> I thought I would use csr or csc as every row and column will have some
>>> values, but not the same each time, so I don't think that coo is what I
>>> need. But I will try lil when I have some time.
>> Suppose you have:
>>
>> row, column, value
>> 0, 10, 1.0
>> 0, 11, 2.0
>> 1, 50, 1.0
>> 1, 55, 3.0
>> 1, 100, 4.0
>> - the values are not the same each time - there are two rows, each with
>> own nonzero columns and values.
>>
>> then, as Nathan wrote:
>>>> In [1]: from scipy import *
>>>> In [2]: from scipy.sparse import *
>>>> In [3]: row = array([0,0,1,1,1])
>>>> In [4]: col = array([10,11,50,55,100])
>>>> In [5]: data = array([1.,2.,1.,3.,4.])
>>>> In [6]: A = coo_matrix((data,(row,col)),dims=(3,3))
>> should construct such a matrix, no?
>> r.

Just remove/correct the dims argument - I left there the original one...

> Yes, it should, I think I should go back to sleep...
> Is there a way to create a sparse matrix from a list of lists directly and a
> separate data array ? Just to know if I have to transform my list of lists
> to the format (row, col).
>
> Matthieu

You mean this?

In [3]: row = [0,0,1,1,1]
In [4]: col = [10,11,50,55,100]
In [5]: rc = [row, col]
In [6]: rc
Out[6]: [[0, 0, 1, 1, 1], [10, 11, 50, 55, 100]]
In [7]: data = array([1.,2.,1.,3.,4.])
In [9]: A = sp.coo_matrix((data,rc))
In [10]: A
Out[10]:
<2x101 sparse matrix of type '<type 'numpy.float64'>'
with 5 stored elements in COOrdinate format>

In [11]: print A
(0, 10)       1.0
(0, 11)       2.0
(1, 50)       1.0
(1, 55)       3.0
(1, 100)      4.0

cheers,
r.
```

More information about the Scipy-dev mailing list