请查看
"SSparseMatrix"包。(该包使用SciPy的稀疏矩阵。)
以下是创建和行选择示例(在Python会话中):
>>> from SSparseMatrix import *
>>> mat = [[1, 0, 0, 3], [4, 0, 0, 5], [0, 3, 0, 5], [0, 0, 1, 0], [0, 0, 0, 5]]
>>> smat = SSparseMatrix(mat)
>>> smat.set_column_names(["a", "b", "c", "d"])
<5x4 SSparseMatrix (sparse matrix with named rows and columns) of type '<class 'numpy.int64'>'
with 8 stored elements in Compressed Sparse Row format, and fill-in 0.4>
>>> smat.set_row_names(["A", "B", "C", "D", "E"])
<5x4 SSparseMatrix (sparse matrix with named rows and columns) of type '<class 'numpy.int64'>'
with 8 stored elements in Compressed Sparse Row format, and fill-in 0.4>
>>> smat.print_matrix()
===================================
| a b c d
-----------------------------------
A | 1 . . 3
B | 4 . . 5
C | . 3 . 5
D | . . 1 .
E | . . . 5
===================================
>>> smat[["A","B"],:].print_matrix()
===================================
| a b c d
-----------------------------------
A | 1 . . 3
B | 4 . . 5
===================================
dtype.names
指的是结构化数组中的fields
。可以将其视为 CSV 表格中的列名/表头。您无法在字段之间进行太多数学计算。而且您不能在稀疏矩阵中使用这种类型的dtype
。 - hpaulj