[SciPy-user] how to copy several rows/columns to a sparse matrix?

Zhang Sam hollowspook at gmail.com
Thu Nov 30 08:47:02 CST 2006


Hi, there

I want to use sparse matrix with scipy. But I can't copy several
rows/columns to a coo_matrix at the same time. The codes is as follows:
------------------------------------------------------------------------------------------------------------------------------------------
>>> from numpy import *
>>> from scipy import *
>>> a=sparse.coo_matrix((array([1,2,3]),[[0,1,2],[0,1,2]]), (3,3))
>>> a
<3x3 sparse matrix of type '<type 'float64scalar'>'
 with 3 stored elements in COOrdinate format>
>>> print a
  (0, 0) 1.0
  (1, 1) 2.0
  (2, 2) 3.0
>>> b = array([[1,2,3],[4,5,6]])
>>> b
array([[1, 2, 3],
       [4, 5, 6]])
>>> a[[0,1],:] = b
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "D:\Python24\lib\site-packages\scipy\sparse\sparse.py", line 237, in
__getattr__
    raise AttributeError, attr + " not found"
AttributeError: __setitem__ not found
-----------------------------------------------------------------------------------------------------------------------------------------------

Maybe it is not very clear. I also show a same type operation however with
numpy arrays.
---------------------------------------------------------------------------------------------
>>> a = arange(8)
>>> a.shape = 2,4
>>> a
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> b = array([4,4,4,4])
>>> b.shape=2,2
>>> b
array([[4, 4],
       [4, 4]])
>>> a[:,[1,2]]=b
>>> a
array([[0, 4, 4, 3],
       [4, 4, 4, 7]])
---------------------------------------------------------------------------------------------

Why this happens? Does sparse matrix defined in scipy need different
operation?


Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20061130/471a6aef/attachment.html 


More information about the SciPy-user mailing list