[SciPy-user] unsupported operand type(s) for +: 'slice' and 'int'
Nils Wagner
nwagner at mecha.uni-stuttgart.de
Tue Mar 7 05:02:14 EST 2006
Ed Schofield wrote:
> 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
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
I followed your advice.
M = dok_matrix((n,n))
Traceback (most recent call last):
File "aispd.py", line 40, in ?
M[:,j] = m_j
File "/usr/lib64/python2.4/site-packages/scipy/sparse/sparse.py", line
1644, in __setitem__
if len(seq) != len(value):
TypeError: __len__() should return an int
>>> M
<420x420 sparse matrix with 0 stored elements in Dictionary Of Keys format>
>>> m_j
<420x1 sparse matrix of type '<type 'float64scalar'>'
with 3 stored elements (space for 2002)
in Compressed Sparse Row format>
Am I missing something ?
Nils
More information about the SciPy-User
mailing list