Large memory usage of scipy.sparse.csr_matrix.toarray()

Viewed 2636

I have a fairly large sparse matrix A as a scipy.sparse.csr_matrix. It has the following properties:

A.shape: (77169, 77169)
A.nnz: 284811011
A.dtype: dtype('float16')

Now I have to convert it to a dense array using .toarray(). My estimate for the memory usage would be

77169**2 * (16./8.) / 1024.**3 = 11.09... GB

which would be fine as my machine has ~48GB of memory. In fact, if I create a=np.ones((77169, 77169), dtype=np.float16) that works fine and indeed a.nbytes/1024.**3 = 11.09.... However, when I run A.toarray() on the sparse matrix it packs all of memory and starts to use the swap at some point (it doesn't raise a MemoryError). Whats going wrong here? Shouldn't it easily fit into my memory?

1 Answers
Related