Python is a programming language none of the programmers is unaware of. Its versatility and understandable coding made its way into the programming world. Numpy is the library of Python which is known for supporting multi-dimensional and large matrices and arrays. When you are working on Python with this library, you may encounter “numpy.core._exceptions.MemoryError: Unable to allocate array with shape”.
If you are just starting with numpy, you can get the error message. There is no need to get panicked as we are here to provide a helping hand to take you out of this issue. Go ahead with the next section to know how you encounter the error
How you encounter “numpy.core._exceptions.MemoryError: Unable to allocate array with shape”
When you are using numpy, the error warning shows like this
Traceback (most recent call last): File "<stdin>", line 1, in <module> numpy.core._exceptions.MemoryError: Unable to allocate array with shape (789412, 78, 98754) and data type uint8
The program code:
nmp.zeros((789412, 78, 98754), dtype='uint8')
Solutions to fix the error message
Solution 1 – change dtype
To solve the “numpy.core._exceptions.MemoryError: Unable to allocate array with shape” error in a simple way, you need to change the dtype to uint8.
Use
mask = nmp.zeros(edges.shape,dtype='uint8')
instead of
mask = nmp.zeros(edges.shape)
Solution 2 – Make the data type changed
To get rid of the error, you need to change the data type to numpy.uint8. check the below code
data['label'] = data['label'].astype(np.uint8)
Solution 3- Change to a 64-bit version from a 32-bit version
When working on Python, you have to change the version from 32-bit to 64-bit. You can use a 32-bit version if you have a 32-bit CPU with 4 GB of RAM.
With the Python version of a 64-bit, the problem can be fixed. Use the code below to ensure the version. With a version of 64-but:
Python 3.7.5rc1 (tags/v3.7.5rc1:4082f600a5, Oct 1 2019, 20:28:14) [MSC v.1916 64 bit (AMD64)]
Solution 4 – Increase pagefile in Windows 10
To fix the error, you need to increase the pagefile in windows 10 with the following steps
- Press the Windows key on your keyboard
- In the search box, type SystemPropertyAdvanced
- From the drop-down option, choose Run an Administrator
- On Performance menu, select settings
- Click Advanced tab and select Change
- Next, you need to uncheck Managing the page file size in all drives automatically
- Select the Custom size to enter the suitable dimensions
- To exit, click OK
- Restart the computer
Solution 5 – Increase pagefile in Windows 8
- Windows 8 users can fix the error using the below steps
- On your keyboard press Windows key + X, then choose System in the Menu
- Open Advanced System Settings. Here, you may need to provide an admin password or confirm your choice
- Next is to select settings under Performance on the Advanced tab
- Choose the Advanced tab under Virtual memory, then click Change
- Clear the Automatically manage paging size of the file for all drive check box
- Choose the drive that has the paging file under the Drive [Volume Label]
- Select Custom size, the initial or maximum size in MB should be added in the size option, then click set and OK
- Restart the system
Conclusion
We have discussed various solutions to fix “numpy.core._exceptions.MemoryError: Unable to allocate array with shape”. All the solutions mentioned above are effective, so you can pick the one that is suitable for you.
I wish you luck!
Reference Source: https://stackoverflow.com/questions/57507832/unable-to-allocate-array-with-shape-and-data-type
https://splunktool.com/unable-to-allocate-array-with-shape-and-data-type
https://www.anycodings.com/1questions/1361358/unable-to-allocate-array-with-shape-and-data-type