【问题标题】:How to handle calculations on huge numpy array to avoid memory allocation error?如何处理巨大的 numpy 数组的计算以避免内存分配错误?
【发布时间】:2020-02-12 13:39:22
【问题描述】:

我需要一个大小为 (62500 x 62500) 的负单位矩阵。 使用 numpy 声明一个正常的单位矩阵就像一个魅力:

eye = np.eye(62500, 62500)

但是,做这样的事情

negative_eye1 = np.negative(np.eye(62500, 62500))
# or
negative_eye2 = np.eye(62500, 62500) * -1

会导致这个错误

无法分配形状为 (62500, 62500) 且数据类型为 float64 的数组

然后在scipy.sparse.bmat() 函数中使用该矩阵,从而生成一个 csr 矩阵,其中内存不再是这样的问题。

如何计算这个矩阵?

【问题讨论】:

  • 你试过了吗:x = np.diag(np.full((6250), -1))

标签: python numpy scipy sparse-matrix


【解决方案1】:

您可以使用scipy.sparse.eye(对角线上的稀疏矩阵):

from scipy import sparse

negative_eye = -sparse.eye(62500, 62500)

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2018-02-12
    • 2021-07-29
    • 1970-01-01
    相关资源
    最近更新 更多