你看到的只是字符串表示调用时使用print(my_csr_mat)
。它列出了(在你的情况下)所有nonzeros在你的矩阵内。 (也许会有大量非零的输出被截断)。
由于这是一个稀疏矩阵,因此它具有二维。
(0, 9) 0.34399327143
意思是:矩阵元素@位置[0,9]是0.34399327143。
小演示:
import numpy as np
from scipy.sparse import csr_matrix
matrix_dense = np.arange(20).reshape(4,5)
zero_out = np.random.choice((0,1), size=(4,5), p=(0.7, 0.3))
matrix_dense_mod = matrix_dense * zero_out
print(matrix_dense_mod)
sparse_mat = csr_matrix(matrix_dense_mod)
print(sparse_mat)
Output:
[[ 0 0 2 0 4]
[ 0 6 0 8 0]
[ 0 11 0 13 14]
[15 0 0 18 19]]
(0, 2) 2
(0, 4) 4
(1, 1) 6
(1, 3) 8
(2, 1) 11
(2, 3) 13
(2, 4) 14
(3, 0) 15
(3, 3) 18
(3, 4) 19
我不确定你的意思So I find on this, but there are no structure as same as the result
,但要注意:scipy.sparse 文档中的大多数示例在打印调用中都有一个 my_mat.toarray() ,这意味着它正在从稀疏矩阵构建一个密集数组,该矩阵具有不同的字符串表示样式.