Memory issue when create a diagonal numpy array

Question:

I have created a diagonal numpy array:

a = numpy.float32(numpy.random.rand(10))
a = numpy.diagonal(a)

However, I face MemoryError since my matrix is extremely large. Is there anyway to save the memory?

Asked By: Hossein Ayouqi

||

Answers:

The best way to handle this case is to create a sparse matrix using scipy.sparse.diags as follows:

a = numpy.float32(numpy.random.rand(10))
a = sparse.diags(a)

If the shape of your diagonal numpy array is n*n, utilizing sparse.diags would result in a matrix n times smaller. Almost all matrix operations are supported for sparse matrices.

Answered By: Hossein Ayouqi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.