实例化numpy数组时出现内存错误 [英] Memory error In instantiating the numpy array
问题描述
我有一个50,000个元素的列表 A ,每个元素都是一个形状数组(102400)
I have a list A of a 50,000 elements and each element is an array of shape (102400)
我尝试实例化数组 B .
B=numpy.array(A)
但这会引发MemoryError异常.我知道内存和大小非常大吗,但是有没有一种方法可以避免numpy中的ndarrays导致此MemoryError ??
But this throws an exception MemoryError.I know that the memory and size is very huge?But is there a way to avoid this MemoryError with ndarrays in numpy ??
推荐答案
作为一种解决方法,您可以避免使用以下方式复制数组:
As a workaround, you could avoid copying the arrays with something like:
B = np.empty(len(A), dtype=object)
for i, a in enumerate(A):
B[i] = a
这将允许创建dtype object
的ndarray,其元素将是A
的原始元素.然后,您可以对其执行 some 数组操作.
This would allow to create a ndarray of dtype object
whose elements would be the original elements of A
. You could then perform some array operations on it.
但是我不确定您是否可以通过这种方式赢得胜利.
But I am not sure you have something to win in going this way.
这篇关于实例化numpy数组时出现内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!