2017-01-24 14 views

回答

2

我有稀疏矩陣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 

,我們可以計算出從形狀,D類和NNZ這些值;例如8字節* 1000,4字節* 1000,4字節* X.shape [0]等。

其他格式需要知道他們的數據存儲方法(例如lil,dok等)。

+0

非常整潔的答案!什麼'X.data .__ sizeof __()'返回你的稀疏矩陣? – MaxU

+1

'48' - numpy數組的典型標題大小(形狀,步幅,標誌等的空間) – hpaulj