Python:修改稀疏数组元素

时间:2014-09-12 01:44:31

标签: python numpy scipy sparse-matrix

以下是Ipython屏幕的副本,其中' Lp'是一个稀疏矩阵:

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

1 个答案:

答案 0 :(得分:3)

这是因为csr_matrix不支持像Lp [0,0]这样的索引,请尝试使用csr_matrix:

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)