python 内存不足错误的解决方法是什么?

Bar*_*ssa 4 python numpy out-of-memory scipy

我正在将 ax,y,z 点文件 (LAS) 读入 python 并遇到内存错误。我正在为我正在处理的项目在已知点之间插入未知点。我开始处理小文件(< 5,000,000 点)并且能够毫无问题地读/写 numpy 数组和 python 列表。我收到了更多要使用的数据(> 50,000,000 点),现在我的代码因 MemoryError 而失败。

处理如此大量的数据有哪些选择?我不必一次将所有数据加载到内存中,但我需要使用scipy kd-tree查看相邻点我在 64 位 Windows XP 操作系统上使用 Python 2.7 32 位。

提前致谢。

编辑:代码发布在下面。我拿出了长计算和变量定义的代码。

from liblas import file
import numpy as np

f = file.File(las_file, mode='r')
num_points = int(f.__len__())
dt = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('i', 'u2'), ('c', 'u1'), ('t', 'datetime64[us]')]
xyzict = np.empty(shape=(num_points,), dtype = dt)
counter = 0
for p in f:
    newrow = (p.x, p.y, p.z, p.intensity, p.classification, p.time)
    xyzict[counter] = newrow    
    counter += 1

dropoutList = []
counter = 0
for i in np.nditer(xyzict):
    # code to define P1x, P1y, P1z, P1t
    if counter != 0:
        # code to calculate n, tDiff, and seconds 
        if n > 1 and n < scanN:
            # code to find v and vD
            for d in range(1, int(n-1)):
                # Code to interpolate x, y, z for points between P0 and P1
                # Append tuple of x, y, and z to dropoutList
                dropoutList.append(vD)
    # code to set x, y, z, t for next iteration
    counter += 1
Run Code Online (Sandbox Code Playgroud)

bog*_*ron 5

无论系统中的 RAM 量如何,如果您运行的是 32 位 python,您的应用程序的实际 RAM 限制约为 2 GB。SO 上还有许多其他问题可以解决这个问题(例如,请参阅此处)。由于您在 ndarray 中使用的结构是 23 个字节,并且您正在读取超过 50,000,000 个点,这已经使您达到大约 1 GB。您尚未包含其余代码,因此不清楚程序的其他部分消耗了多少额外内存。

如果您的系统中有超过 2 GB 的 RAM 并且您将继续处理大型数据集,您应该安装 64 位 python 来解决这个 ~ 2 GB 的限制。