scipy 支持目前主流的 稀疏矩阵 储存格式,包括bsr_matrix,coo_matrix,csc_matrix,csr_matrix,dia_matrix,dok_matrixlil_matrix,具体参见文档。
COOrdinate format
仅存储非零元素以及每个非零元素的坐标。使用3个数组进行存储:values, row, column
- values: 数据,包括矩阵中的非零元素, 顺序任意。
- rows: 对应数据所处的行。
- columns: 对应数据所处的列。
示例:
# Constructing a matrix using ijv format
>>> row = np.array([0, 3, 1, 0])
>>> col = np.array([0, 3, 1, 2])
>>> data = np.array([4, 5, 7, 9])
>>> coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[4, 0, 9, 0],
[0, 7, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 5]])
COO储存优点:简单、容易理解。
Ref. https://blog.csdn.net/anshan1984/article/details/8580952
0 条评论