【问题标题】:MemoryError with numpy where内存错误与 numpy where
【发布时间】:2016-03-30 12:57:15
【问题描述】:

我收到了MemoryErrornumpy.where,但我不知道为什么。我无法在此处发布实际代码,但下面是一个复制该问题的小型工作示例。

import numpy as np
dat = np.random.randn(100000, 1, 1, 1, 45, 2, 3)
# The following two steps seem superfluous but I wanted to replicate
# behaviour in the original code    
cond = dat[:,0,0,0,0,0,0] > 0
cond = cond[:,None,None,None,None,None,None]

dat2 = np.where(cond, dat, 0)
dat[...,2] = np.where(cond, dat[...,2], dat2[...,2]) # Causes MemoryError

我知道向我的计算机添加更多内存可以解决问题,但我想了解这里发生了什么。

我希望上面的数组切片不会复制数组而只会返回一个视图,但我想它实际上是出于某种原因复制数组。

【问题讨论】:

    标签: python arrays numpy memory


    【解决方案1】:

    这里没有“魔法”,您使用np.random.randn(100000, 1, 1, 1, 45, 2, 3) 创建的数据数组非常大。

    Numpy 似乎将每个数字存储为 64 位(8 字节)浮点数,因此您的数组占用大约 206 兆字节的内存 (100000 * 1 * 1 * 1 * 45 * 2 * 3 * 8)。

    /usr/bin/time -v python test.py 表示该程序在峰值时使用了大约 580 MB,这可能是由于复制了对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2021-03-03
      相关资源
      最近更新 更多