+2 votes
in Programming Languages by (77.2k points)

I am initializing a matrix of size (3342590, 25110) with zeroes. 

Here is the code that is throwing the error message: numpy.core._exceptions._ArrayMemoryError: Unable to allocate 625. GiB for an array with shape (3342590, 25110) and data type float64

array = np.zeros((3342590, 25110))

How to fix this error? 

1 Answer

+2 votes
by (60.0k points)
selected by
 
Best answer

Your matrix initialization is trying to acquire 625gb of the memory and you do not have that much memory on your system. That's why you are getting the error.

The easy fix is to add dtype with the zeros() function. If your numbers in the matrix are not going to be very big, you can try float16.

Here is an example:

import numpy as np

array = np.zeros((3342590, 25110), dtype=np.float16)

If your matrix values are going to be very small, you can use int8 as dtype. 


...