简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM