[英]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.