36

Is it possible to determine the byte size of a scipy.sparse matrix? In NumPy you can determine the size of an array by doing the following:

import numpy as np

print(np.zeros((100, 100, 100).nbytes)
8000000
BenMorel
  • 34,448
  • 50
  • 182
  • 322
ebressert
  • 2,319
  • 4
  • 21
  • 27

1 Answers1

61

A sparse matrix is constructed from regular numpy arrays, so you can get the byte count for any of these just as you would a regular array.

If you just want the number of bytes of the array elements:

>>> from scipy.sparse import csr_matrix
>>> a = csr_matrix(np.arange(12).reshape((4,3)))
>>> a.data.nbytes
88

If you want the byte counts of all arrays required to build the sparse matrix, then I think you want:

>>> print a.data.nbytes + a.indptr.nbytes + a.indices.nbytes
152
user545424
  • 15,713
  • 11
  • 56
  • 70
  • Nice, that worked very well. I'm more interested in the first case, but the second case is interesting. Does that memory size represent the entire object? – ebressert Jun 23 '12 at 21:25
  • No, just the arrays. See http://stackoverflow.com/questions/33978/find-out-how-much-memory-is-being-used-by-an-object-in-python. – user545424 Jun 23 '12 at 21:33
  • 5
    For coo_matrix, it should be a.col.nbytes + a.row.nbytes + a.data.nbytes – czxttkl Sep 01 '16 at 15:57
  • 1
    Isn't there a generic way that works for all types of sparse matrices? Thanks! – Martin Sep 16 '21 at 08:49
  • Best solution I found so far is through pickles: https://stackoverflow.com/a/59228005/1822138 – Martin Sep 16 '21 at 08:55