【发布时间】:2016-12-28 18:56:39
【问题描述】:
我在 python 中有以下代码:
import numpy as np
from scipy.sparse import csr_matrix
M = csr_matrix(np.ones([2, 2],dtype=np.int32))
print(M)
print(M.data.shape)
for i in range(np.shape(mat)[0]):
for j in range(np.shape(mat)[1]):
if i==j:
M[i,j] = 0
print(M)
print(M.data.shape)
前 2 次打印的输出是:
(0, 0) 1
(0, 1) 1
(1, 0) 1
(1, 1) 1
(4,)
代码正在更改同一索引 (i==j) 的值并将该值设置为零。 执行循环后,最后 2 次打印的输出为:
(0, 0) 0
(0, 1) 1
(1, 0) 1
(1, 1) 0
(4,)
如果我正确理解了稀疏矩阵的概念,那不应该是这样。它不应该显示零值,最后 2 次打印的输出应该是这样的:
(0, 1) 1
(1, 0) 1
(2,)
有人对此有解释吗?我做错了吗?
【问题讨论】:
-
我也被这个弄糊涂了,问了这个问题:stackoverflow.com/questions/19122024/…