【发布时间】:2014-08-06 22:55:13
【问题描述】:
我有一个3000x6000 2D 网格(来自 tiff 图像)。我想使用scipy.interpolate 库中的griddata 方法将其重新网格化为较低分辨率的网格。首先,我需要根据我读到的here 形成一个18000000x2 numpy array 作为griddata 的输入。这是我的工作:
import numpy as np
from scipy.interpolate import griddata
x_length = 6000
y_length = 3000
def func(x, y):
return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
grid_x, grid_y = np.meshgrid(np.linspace(0,1,x_length),np.linspace(0,1,y_length))
points = np.random.rand(x_length*y_length, 2)
values = func(points[:,0], points[:,1])
grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
我在做griddata 时得到一个MemoryError。我有 8 GB 的 RAM,根据this question 的第一个答案,我不应该收到此错误。
总体而言,将3000x6000 网格重新设置为较低分辨率的网格应该不难,我想我在这里做了一些有趣的事情。我应该让 eMemoryError 使用 8 GB RAM 执行这些代码留置权吗?
P.S:虽然我有一个64-bit 操作系统(Windows 7),但我使用的是以下 Python 版本:
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
【问题讨论】:
-
它对我有用。我有 4 GB 的 RAM,并且已经占用了超过 3 个。事实上,在 htop 中我只看到大约 200 MB,而您的每个数组正好是 288 MB。
-
另外,使用
x = np.round(x, 5)舍入到小数的前 5 位。 -
@Davidmh 我对问题进行了相当多的编辑。我仍然收到运行代码的
MemoryError。你有错误吗? -
不,还是不明白。使用量约为 2GB。
-
这应该为您提供 Python 可访问的所有数组的大小:
1e-6 * sum(x.nbytes for x in globals().values() if isinstance(x, np.ndarray))。正如预期的那样,我得到了和以前一样的结果,720 MB。
标签: python numpy memory-leaks grid scipy