7

I would like to convert a list with shape (1200, 140, 150, 130) to a numpy array, but the standard numpydata = np.array(mylist) uses to much memory.

Is there any less memory consuming way to do this?

5
  • 1
    What do you expect from numpy's dense arrays and your 3276000000 elements? Commented Feb 10, 2020 at 16:46
  • @sascha I bet there are even bigger arrays out there and I have about 100GB of RAM. Commented Feb 10, 2020 at 16:50
  • 1
    Your list will very likely already use up way more memory than the array will. Probably better to avoid creating the list in the first place. Commented Feb 10, 2020 at 16:51
  • 1
    Can you create np.zeros((1200,140,150,130)), that is an array of the required size? That test whether there's memory for your result. It certainly possible that np.array makes some temporary copy(s) of the input, since it has to read the whole thing to figure out shape and eventual dtype (and possibly convert elements to common dtype). So there's a lot going on in compiled code. Iterating on the list and assigning individual (140,150,130) arrays to the zeros might reduce the memory use. With large arrays there's often a trade off between iteration and memory management. Commented Feb 10, 2020 at 17:57
  • egal, did you solve the problem? Commented Aug 27, 2022 at 16:30

1 Answer 1

3

If there's memory for the final result, but np.array internals is using too much memory, you might get around that processing the list in blocks. For example:

In [236]: res = np.zeros((10,3,4),int)                                                         
In [237]: alist = np.random.randint(0,10,(10,3,4)).tolist()                                    
In [238]: for i,row in enumerate(alist): 
     ...:     res[i] = row 
In [240]: np.allclose(res, np.array(alist))                                                    
Out[240]: True

For small arrays this iteration will be slower, but with large ones, memory management issues might out weight the iteration costs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.