Search code examples
pythonarraysnumpyout-of-memory

Memory issue when create a diagonal numpy array


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?


Solution

  • 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.