将列表转换为 numpy 数组而不使用太多 RAM

ega*_*gal 5 python numpy

我想将形状为 (1200, 140, 150, 130) 的列表转换为 numpy 数组,但标准numpydata = np.array(mylist)会占用大量内存。

有没有消耗内存更少的方法来做到这一点?

hpa*_*ulj 3

如果最终结果有内存,但np.array内部使用了太多内存,您可能会绕过按块处理列表的问题。例如:

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
Run Code Online (Sandbox Code Playgroud)

对于小型数组,此迭代会较慢,但对于大型数组,内存管理问题可能会超过迭代成本。