繁体   English   中英

Python:修改稀疏数组元素

[英]Python: modify a sparse array element

以下是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

这是因为bsr_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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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