简体   繁体   English

MemoryError with numpy where

[英]MemoryError with numpy where

I'm getting a MemoryError with numpy.where but I'm not sure why.我收到了numpy.whereMemoryError ,但我不知道为什么。 I can't post the actual code here, but below is a small working example that replicates the issue.我无法在此处发布实际代码,但下面是一个复制该问题的小型工作示例。

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

I understand that adding more memory to my computer will solve the issue, but I would like to understand what is going on here.我知道向我的计算机添加更多内存将解决问题,但我想了解这里发生了什么。

I expect the array slices above will not copy the array but only return a view, but I suppose that it is actually copying the array for some reason.我希望上面的数组切片不会复制数组而只会返回一个视图,但我认为它实际上是出于某种原因复制了数组。

There is no "magic" going on here, your data array that you create using np.random.randn(100000, 1, 1, 1, 45, 2, 3) is very large.这里没有“魔法”,您使用np.random.randn(100000, 1, 1, 1, 45, 2, 3)创建的数据数组非常大。

Numpy seems to store each number as 64 bit (8 byte) float, so your array takes up around 206 Megabytes of memory (100000 * 1 * 1 * 1 * 45 * 2 * 3 * 8). Numpy 似乎将每个数字存储为 64 位(8 字节)浮点数,因此您的数组占用大约 206 兆字节的内存(100000 * 1 * 1 * 1 * 45 * 2 * 3 * 8)。

/usr/bin/time -v python test.py says that the program uses around 580 MB at its peak, which might be due to copying the object. /usr/bin/time -v python test.py表示该程序在其峰值使用了大约 580 MB,这可能是由于复制了对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM