无法理解 scipy.sparse.csr_matrix 示例

我无法csr_matrix理解 scipy 文档中的示例:https ://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html


有人可以解释这个例子是如何工作的吗?


>>> row = np.array([0, 0, 1, 2, 2, 2])

>>> col = np.array([0, 2, 2, 0, 1, 2])

>>> data = np.array([1, 2, 3, 4, 5, 6])

>>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()

array([[1, 0, 2],

       [0, 0, 3],

       [4, 5, 6]])

我相信这是遵循这种格式。


csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])


where data, row_ind and col_ind satisfy the relationship a[row_ind[k], col_ind[k]] = data[k].


什么是a在这里吗?


月关宝盒
浏览 393回答 3
3回答

互换的青春

这是一个稀疏矩阵。因此,它将显式索引和值存储在这些索引处。例如,因为 row=0 和 col=0 对应于 1 (示例中所有三个数组的第一个条目)。因此,矩阵的 [0,0] 条目是 1。依此类推。

尚方宝剑之说

row = np.array([0, 0, 1, 2, 2, 2])col = np.array([0, 2, 2, 0, 1, 2])data = np.array([1, 2] , 3, 4, 5, 6])来自上述数组;对于 k 在 0~5a[row_ind[k], col_ind[k]] = data[k]                  a row[0],col[0] = [0,0] = 1 (from data[0])  row[1],col[1] = [0,2] = 2 (from data[1])  row[2],col[2] = [1,2] = 3 (from data[2])  row[3],col[3] = [2,0] = 4 (from data[3])  row[4],col[4] = [2,1] = 5 (from data[4])  row[5],col[5] = [2,2] = 6 (from data[5])所以让我们排列矩阵'a'的形状(3X3)a   0  1  20 [1, 0, 2]  1 [0, 0, 3]  2 [4, 5, 6]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python