假设我想监控我的 SciPy 稀疏矩阵 mat 占用的内存。在 NumPy 中我会利用 nbytes 属性,但在 SciPy 中似乎没有那样的东西。 我怎样才能检索到这些信息?
请您参考如下方法:
我有稀疏矩阵X
In [605]: X
Out[605]:
<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 1000 stored elements in Compressed Sparse Row format>
getsizeof 没有告诉我任何有用的信息
In [606]: import sys
In [607]: sys.getsizeof(X)
Out[607]: 28
对于存储在 3 个数组中的 csr 矩阵,稀疏数据和索引是:
In [612]: X.data.nbytes
Out[612]: 8000
In [613]: X.indices.nbytes
Out[613]: 4000
In [614]: X.indptr.nbytes
Out[614]: 404
因此,总空间大致就是这些值的总和。
对于coo格式
In [615]: Xc=X.tocoo()
In [616]: Xc.data.nbytes
Out[616]: 8000
In [617]: Xc.row.nbytes
Out[617]: 4000
In [618]: Xc.col.nbytes
Out[618]: 4000
我们可以根据 shape、dtype 和 nnz 计算这些值;例如8字节*1000、4字节*1000、4字节*X.shape[0]等
其他格式需要了解它们的数据存储方法(例如lil、dok 等)。
