我有一组位于X、Y网格上的高程测量值。我正在尝试创建一个通过立面的切片(在一个角度下,所以在网格点上不完美(。我想过使用scipy的2D插值方法,但我得到了错误OverflowError:太多的数据点无法插值。我没有一个庞大的数组,所以我想知道为什么会出错。
我的数据:
>>> XX.shape, YY.shape, depth_array.shape
((787, 1858), (787, 1858), (787, 1858))
>>> XX
array([[ 0, 0, 0, ..., 0, 0, 0],
[ 1, 1, 1, ..., 1, 1, 1],
[ 2, 2, 2, ..., 2, 2, 2],
...,
[784, 784, 784, ..., 784, 784, 784],
[785, 785, 785, ..., 785, 785, 785],
[786, 786, 786, ..., 786, 786, 786]])
>>> YY
array([[ 0, 1, 2, ..., 1855, 1856, 1857],
[ 0, 1, 2, ..., 1855, 1856, 1857],
[ 0, 1, 2, ..., 1855, 1856, 1857],
...,
[ 0, 1, 2, ..., 1855, 1856, 1857],
[ 0, 1, 2, ..., 1855, 1856, 1857],
[ 0, 1, 2, ..., 1855, 1856, 1857]])
>>> depth_array
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]])
# The depth array seems empty, but that's not the case (but that are quite a few zero values)
>>> interpolate.interp2d(YY, XX, depth_array, kind='linear')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/yorian/.pyenv/versions/3.7.5/envs/euromax/lib/python3.7/site-packages/scipy/interpolate/interpolate.py", line 229, in __init__
self.tck = fitpack.bisplrep(x, y, z, kx=kx, ky=ky, s=0.0)
File "/Users/yorian/.pyenv/versions/3.7.5/envs/euromax/lib/python3.7/site-packages/scipy/interpolate/_fitpack_impl.py", line 956, in bisplrep
msg=msg)
File "/Users/yorian/.pyenv/versions/3.7.5/envs/euromax/lib/python3.7/site-packages/scipy/interpolate/_fitpack_impl.py", line 48, in _int_overflow
raise OverflowError(msg)
OverflowError: Too many data points to interpolate
我现在使用RectBivariateSpline,但它似乎适合样条曲线,我想要一个2D线性插值。(7841858(分真的太多了吗?如果是,我该如何实现?
如果您有一个正则网格,那么只为x和y坐标提供1D数组就足够了。这计算成本较低,但我不知道这是否是通用网格中出现错误消息的原因。
import numpy as np
from scipy import interpolate
nx = 787
ny = 1858
depth_array = np.random.random((ny, nx))
res = interpolate.interp2d(range(nx), range(ny), depth_array, kind='linear')
我试图重现您的错误,并在使用通用网格x, y = np.meshgrid(np.arange(nx), np.arange(ny))
:时大致发现了这种行为
nx*ny < 200000
:作品nx*ny > 200000
:内存错误nx*ny > 250000
:溢流错误