egal egal
egal egal

Reputation: 71

Convert list to numpy array without using much RAM

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?

Upvotes: 7

Views: 1481

Answers (1)

hpaulj
hpaulj

Reputation: 231385

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.

Upvotes: 3

Related Questions