[SciPy-user] unsupported operand type(s) for +: 'slice' and 'int'

Ed Schofield schofield at ftw.at
Tue Mar 7 03:52:52 CST 2006


Nils Wagner wrote:
> Traceback (most recent call last):
>   File "aispd.py", line 33, in ?
>     M[:,j] = m_j
>   File "/usr/lib64/python2.4/site-packages/scipy/sparse/sparse.py", line 
> 1308, in __setitem__
>     self.indptr = resize1d(self.indptr, row+2)
> TypeError: unsupported operand type(s) for +: 'slice' and 'int'
>  >>> M
> <420x420 sparse matrix of type '<type 'float64scalar'>'
>         with 0 stored elements (space for 100)
>         in Compressed Sparse Row format>
>  >>> m_j
> <420x1 sparse matrix of type '<type 'float64scalar'>'
>         with 10 stored elements (space for 1002)
>         in Compressed Sparse Row format>
>
> How can I resolve this problem ?
>
>  
>
> for i in arange(0,420):
>      M[i,j] = m_j[i]
>
> seems to be not very efficient.
>   
There's only support for slicing lil_matrix and dok_matrix objects.  All
sparse matrix manipulations like your for loop are much more efficient
with these objects.  You're trying to do column-wise slicing and
lil_matrix is a row-wise format, so I suggest you use dok_matrix.  Only
convert to CSR or CSC after you've finished modifying its elements, for
multiplication, solvers, etc.

-- Ed



More information about the SciPy-user mailing list