[SciPy-dev] feedback on scipy.sparse

Robert Cimrman cimrman3@ntc.zcu...
Thu Dec 13 04:51:56 CST 2007


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.

> 2007/12/13, Nathan Bell <wnbell@gmail.com>:
>> On Dec 12, 2007 4:03 AM, Stefan van der Walt <stefan@sun.ac.za> wrote:
>>>> I'd like to know if some fancy indexing will be available soon. I
>> explain
>>>> myself. I need to populate a sparse matrix with some weights for each
>> line. I'd
>>>> like to do s[i, indices] = weights but it does not seem to work. I
>> could use a
>>>> loop, but it would be slower and this is not acceptable (as it is
>> possible to
>>>> do so in Matlab).
>>> The lil_matrix is used to construct matrices like that:
>> Matthieu, if you know all the row and column indices and their
>> corresponding values then you can use the coo_matrix format like this:
>>
>> In [1]: from scipy import *
>> In [2]: from scipy.sparse import *
>> In [3]: row = array([0,1,2,2,1])
>> In [4]: col = array([1,2,0,1,1])
>> In [5]: data = array([1,2,3,4,5])
>> In [6]: A = coo_matrix((data,(row,col)),dims=(3,3))
>> In [7]: A.todense()
>> Out[7]:
>> matrix([[0, 1, 0],
>>         [0, 5, 2],
>>         [3, 4, 0]])
>>



More information about the Scipy-dev mailing list