Error saving and loading a list of matrices

2024/11/15 3:32:39

I have a list "data_list", and I would save it in order to load it in another script. First of all I converted it in an array, in this way:

data_array = np.array(data_list)

Then I saved it:

np.savez("File", data_array)

Then, in another script I want to access to "File"; so:

a = np.load("File.npz") 
b = a['arr_0']

I used this code until two weeks ago and it worked fine. In these days I am trying to work with my program, but it ends with an error identified in the line

b = a['arr_0']

"File" is a 300 MB file. The strangest thing is that it has stopped suddenly to work. Any idea about what can be happened?

Ps: I give you some information. My list contains 180 matrices 511x511. Each matrix contains decimal numbers (I tried to create 180 matrices of zeros, and the error occurs in the same way). If I reduce the number of matrices, the script works fine: in particular down to 130 matrices it is ok, while up to the program doesn't work. Here I report the error message

        b = a['arr_0']File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 241, in     __getitem__return format.read_array(value)File "C:\Python27\lib\site-packages\numpy\lib\format.py", line 459, in   read_arrayarray = numpy.fromstring(data, dtype=dtype, count=count)MemoryError 
Answer

MemoryError is an out of memory condition. This explains why it happens with objects of at least a certain size - more and bigger arrays, as you would expect, require more memory. What the max size is, and why it seems to have changed, is harder. This can be highly specific to your system, especially in regard to considerations like:

  • How much memory (physical RAM and swap space) exists and is available to the operating system
  • How much virtual memory the OS gives to Python
  • How much of that you're already using
  • The implementation of the C library, especially of its malloc function, which can affect how Python uses the memory it is allocated

And possibly quite a few other things.

Per the comments, it seems the biggest problem here is that you are running a 32 bit build of Python. On Windows, 32 bit processes apparently have an effective maximum memory address space of around 2GB. By my tests, the list of arrays you are using by itself might take around a quarter of that. The fact that your error only comes up when reading the file back in suggests that numpy deserialisation is relatively memory intensive, but I don't know enough about its implementation to be able to say why that would be. In any case, it seems like installing a 64 bit build of Python is your best bet.

https://en.xdnf.cn/q/119087.html

Related Q&A

Trying to interact with HTML page elements, but none of them are found

Im trying to scrape a webpage using Selenium, but when I try to pass the XPath of a button, I get an error saying that this element does not exist. I tried with another website, and it worked perfectly…

Duplicating an XML element and adding it to a specific position in XML file using python

I have a xml file in which content looks like this: xml_content_to_search = <Document ProviderID="TD" DecimalMarker="comma" Website="https://erc-viewer.sap.com/"> &l…

How do I fix this Gets server error, which is causing display issues?

The list in the left column of ontariocourts311.ca, along with the body of the page under the image intermittently fail to display (which is fixed by refreshing the page). Im a Noob, and have tried var…

Installing Scipy for Windows

I am trying to install Scipy on my computer. I did it by using the command pip install Scipy. (pip & numpy are up-to-date and I am using Python 3.6) I also tried it with Pycharm, but it didnt worke…

Python Opencv, dont put circle on the video

I wrote the following script with OpenCVimport cv2 import numpy as npcap = cv2.VideoCapture(0) ix, iy = -1, -1def draw_circle(event, x, y, flags, param):global ixglobal iyix,iy = x,yif event == cv2.EVE…

List coordinates between a set of coordinates

This should be fairly easy, but Im getting a headache from trying to figure it out. I want to list all the coordinates between two points. Like so:1: (1,1) 2: (1,3) In between: (1,2)Or1: (1,1) 2: (5,1)…

NA values in column is not NaN Pandas Python [duplicate]

This question already has answers here:Prevent pandas from interpreting NA as NaN in a string(7 answers)Closed 2 years ago.I got a CSV File. I got a column Product. One of the products in it, called NA…

How to fix pandas column data

Workflow is :Read CSV file using Pythons pandas library and get Variation Column Variation Column data isVariation ---------- Color Family : Black, Size:Int:L Color Family : Blue, Size:Int:M Color Fam…

Connect to Oracle Database and export data as CSV using Python

I want to connect oracle database to python and using select statement whatever result i will get, I want that result to be exported as csv file in sftp location. I know we can connect oracle with pyth…

Pandas data frame: convert Int column into binary in python

I have dataframe eg. like below Event[EVENT_ID] = [ 4162, 4161, 4160, 4159,4158, 4157, 4156, 4155, 4154]need to convert each row word to binary. Event[b]=bin(Event[EVENT_ID]) doesnt work TypeError: can…