safex
safex

Reputation: 2514

Get memory usage of COO sparse matrix

How can I infer the memory usage of a sparse COO format matrix

Data

row  = np.array([0, 0, 1, 3, 1, 0, 0])
col  = np.array([0, 2, 1, 3, 1, 0, 0])
data = np.array([1, 1, 1, 1, 1, 1, 1])
mat = coo_matrix((data, (row, col)), shape=(4, 4))

Online I found solutions that don't seem to work with coo martrices:

mat.data.nbytes + mat.indptr.nbytes + mat.indices.nbytes

only mat.data.nbytes does not return an error, but the size is much smaller than it should be (when creating a large sparse matrix from dataframe columns). Am I missing any components here?

Upvotes: 0

Views: 409

Answers (1)

sascha
sascha

Reputation: 33532

Read the docs more carefully -> see Attributes

It's

mat.data.nbytes + mat.row.nbytes + mat.col.nbytes

You copy-pasted something tuned for csr/csc matrices (see their Attributes).

Upvotes: 2

Related Questions