三次样条记忆错误 [英] Cubic spline memory error
问题描述
在具有4GB内存的计算机上,这种简单的插值会导致内存错误:
On a computer with 4GB of memory this simple interpolation leads to a memory error:
(基于: 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')
我曾考虑过将数据切成块,但是有没有一种方法可以执行我的三次样条插值而不需要那么多的内存呢? 为什么它甚至会遇到麻烦?
I thought about cutting the data into chunks, but is there a way I can perform this cubic spline interpolation without requiring so much memory? Why does it even get in trouble?
推荐答案
如果在发生错误时查看回溯,则会看到类似以下内容的
If you look at the traceback when the error occurs, you'll see something like:
---------------------------------------------------------------------------
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)
.此函数创建形状为(len(xk), len(xk) + order - 1)
的64位浮点数的二维数组.就您而言,已超过51GB.
The function that is failing is scipy.interpolate._fitpack._bsplmat(order, xk)
. This function creates a 2-d array of 64-bit floats with shape (len(xk), len(xk) + order - 1)
. In your case, this is over 51GB.
代替interp1d
,查看是否 InterpolatedUnivariateSpline
为您工作.例如,
Instead of interp1d
, see if InterpolatedUnivariateSpline
works for you. For example,
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)
我对此没有记忆错误.
这篇关于三次样条记忆错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!