def(matrix, type="csc"): & 大专栏稀疏矩阵的表示法#34;"" create compressed sparse column format (csc) or row format (csr)""" non_zero = [] indices = [] pointer = [0] count = 0 if type == "csr": for i in range(len(matrix)): for j in range(len(matrix[i])): if matrix[i][j] != 0: non_zero.append(matrix[i][j]) indices.append(j) # 添加所在列 count += 1# 计数 pointer.append(count) # 每行都要添加非零值指针 else: for j in range(len(matrix[0])): for i in range(len(matrix)): if matrix[i][j] != 0: non_zero.append(matrix[i][j]) indices.append(j) # 添加所在列 count += 1# 计数 pointer.append(count) # 每行都要添加非零值指针
description = "compressed sparse {type} format, x is non zeros, indices is the {type} pointer, line_num is the {type} line number in the matrix".format(type="row"if type == "csr"else"column") return { "x": non_zero, "indices": indices, "pointer": pointer, "type": type, "description": description "dim": [len(matrix), len(matrix[0])] } }
基于上面的函数得到的 Compressed sparse column format 为:
1 2 3
x [5, 8, 6, 3] indices [1, 1, 3, 2] pointer [0, 1, 3, 4, 4]
而 R 中得到的 Compressed sparse column format 为:
1 2 3 4 5 6 7 8 9
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:4] 1132 ..@ p : int [1:5] 01344 ..@ Dim : int [1:2] 44 ..@ Dimnames:List of 2 .. ..$ : NULL .. ..$ : NULL ..@ x : num [1:4] 5863 ..@ factors : list()
同样的,我们看一下 csr 是否同上面分析的一致。
1 2 3
x [5, 8, 3, 6] indices [0, 1, 2, 1] pointer [0, 0, 2, 3, 4]