python - 创建非常大的 numpy 数组时出现 MemoryError

标签 python arrays numpy

<分区>

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

import numpy as np

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

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

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

最佳答案

研究在 scipy 中使用稀疏数组功能:
scipy.sparse documentation .

这里有一组关于 scipy.sparse 库的示例和教程:
Scipy lecture notes: Sparse Matrices in SciPy

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


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

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?

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])

关于python - 创建非常大的 numpy 数组时出现 MemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213750/

相关文章:

python - 用 libclang 解析;无法解析某些标记(Windows 中的 Python)

python - 选择对象

java - 未输出正确的均值或方差

arrays - Swift 4 无法使用类型的参数列表调用 'index'

python - 不定期重采样

python - numpy:将图像形状从 224 x 224 x 3 更改为 3 x 224 x 224 的最快方法

python - Flask、SQLAlchemy 和流响应时的高内存使用率

python - Pyramid_simpleform 可以与 python3.5.2 一起使用吗?

c - 为什么迭代数字中的每个数字并发现均分数字不起作用?

python-2.7 - read_csv 在 Python 中使用 pandas 将 3 列日期转换为 1 列日期格式,例如 YYYY-MM-DD