user3015347
user3015347

Reputation: 513

Python: modify a sparse array element

The following is a copy of Ipython screen, where 'Lp' is a sparse matrix:

Lp
Out[198]: 
<9x9 sparse matrix of type '<type 'numpy.float64'>'
    with 63 stored elements (blocksize = 3x3) in Block Sparse Row format>

Lp[0,0]
Traceback (most recent call last):

  File "<ipython-input-199-b843d0976d55>", line 1, in <module>
    Lp[0,0]

  File "C:\Users\chensy\Anaconda\lib\site-packages\scipy\sparse\bsr.py", line 299, in __getitem__
    raise NotImplementedError

NotImplementedError

Upvotes: 0

Views: 199

Answers (1)

Shockley
Shockley

Reputation: 307

This is because bsr_matrix doesnt support indexing like Lp[0,0], try using csr_matrix instead:

from scipy.sparse import csr_matrix
Lp = csr_matrix(Lp)
# do modifications
Lp[0,0] = -5.2
# switch back to bsr_matrix
Lp = bsr_matrix(Lp)

Upvotes: 3

Related Questions