python - 三次样条内存错误

标签 python numpy scipy interpolation

在具有 4GB 内存的计算机上,这种简单的插值会导致内存错误:

(基于:http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html)

import numpy as np
from scipy.interpolate import interp1d

x = np.linspace(0, 10, 80000)
y = np.cos(-x**2/8.0)
f2 = interp1d(x, y, kind='cubic')

我考虑过将数据分成 block ,但有没有一种方法可以在不需要那么多内存的情况下执行此三次样条插值? 为什么它甚至会遇到麻烦?

最佳答案

如果您在错误发生时查看回溯,您会看到如下内容:

---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-4-1e538e8d766e> in <module>()
----> 1 f2 = interp1d(x, y, kind='cubic')

/home/warren/local_scipy/lib/python2.7/site-packages/scipy/interpolate/interpolate.py in __init__(self, x, y, kind, axis, copy, bounds_error, fill_value)
    390         else:
    391             minval = order + 1
--> 392             self._spline = splmake(x, y, order=order)
    393             self._call = self.__class__._call_spline
    394 

/home/warren/local_scipy/lib/python2.7/site-packages/scipy/interpolate/interpolate.py in splmake(xk, yk, order, kind, conds)
   1754 
   1755     # the constraint matrix
-> 1756     B = _fitpack._bsplmat(order, xk)
   1757     coefs = func(xk, yk, order, conds, B)
   1758     return xk, coefs, order

MemoryError: 

失败的函数是 scipy.interpolate._fitpack._bsplmat(order, xk)。此函数创建一个 64 位 float 的二维数组,形状为 (len(xk), len(xk) + order - 1)。在您的情况下,这超过了 51GB。

而不是 interp1d,看看是否 InterpolatedUnivariateSpline为你工作。例如,

import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline

x = np.linspace(0, 10, 80000)
y = np.cos(-x**2/8.0)
f2 = InterpolatedUnivariateSpline(x, y, k=3)

我没有遇到内存错误。

关于python - 三次样条内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21435648/

相关文章:

python - 在 Python/NumPy 中计算平均值的元素排列

python - Python/scipy 中的一维非最大抑制

python - 使用 scipy.io 时出现导入错误

python - `sock.recv()` 当连接在非阻塞套接字上死亡时返回空字符串

在 Matlab 中转置的 Python 创建的 HDF5 数据集

Python Numpy recarray 排序双向

numpy - 任意轮廓的平滑样条表示,f(length) --> x,y

python - 交互式 Altair 绘图轴上方的文本

python - Word2Vec 和 Gensim 参数等价

python - chalice 中缺少公钥文件