Python 创建一个非常大的 numpy 数组时出现 MemoryError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37213750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:03:19  来源:igfitidea点击:

MemoryError when creating a very large numpy array

pythonarraysnumpy

提问by Andrew Earl

I'm trying to create a very large numpy array of zeros and then copy values from another array into the large array of zeros. I am using Pycharm and I keep getting: MemoryErroreven when I try and only create the array. Here is how I've tried to create the array of zeros:

我正在尝试创建一个非常大的 numpy 零数组,然后将值从另一个数组复制到大零数组中。我正在使用 Pycharm 并且我不断得到:MemoryError即使我尝试只创建数组。这是我尝试创建零数组的方法:

import numpy as np

last_array = np.zeros((211148,211148))

I've tried increasing the memory heap in Pycharm from 750m to 1024m as per this question: https://superuser.com/questions/919204/how-can-i-increase-the-memory-heap-in-pycharm, but that doesn't seem to help.

我已经尝试按照这个问题将 Pycharm 中的内存堆从 750m 增加到 1024m:https://superuser.com/questions/919204/how-can-i-increase-the-memory-heap-in-pycharm ,但是这似乎没有帮助。

Let me know if you'd like any further clarification. Thanks!

如果您需要进一步说明,请告诉我。谢谢!

采纳答案by amicitas

Look into using the sparse array capabilities within scipy:
scipy.sparse documentation.

研究使用 scipy 中的稀疏数组功能:
scipy.sparse 文档

There are a set of examples and tutorials on the scipy.sparse library here:
Scipy lecture notes: Sparse Matrices in SciPy

这里有一组关于 scipy.sparse 库的示例和教程:
Scipy 讲义:SciPy 中的稀疏矩阵

This may help you solve your memory issues, as well as make everything run faster.

这可以帮助您解决内存问题,并使一切运行得更快。



To create an empty sparse array with values in certain positions as you asked in your comment:

要按照您在评论中的要求,在某些位置创建一个具有值的空稀疏数组:

Is there any way to create an empty array with values in certain positions, such as: last_array[211147][9] but everywhere else would be empty?

有没有办法在某些位置创建一个空数组,例如:last_array[211147][9] 但其他地方都是空的?

from scipy.sparse import *
values = [42]
row_ind = [211147]
col_ind = [9] 
last_array = csc_matrix((values, (row_ind, col_ind)), shape=(211148,211148))

print(last_array[211147,9])