scipy.interpolate.interp2d:我的数据点真的太多了吗?

scipy.interpolate.interp2d: do I really have too many data points?

我有一组位于 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,但这似乎适合样条曲线,我想要二维线性插值。 (787, 1858) 点真的太多了吗?如果是这样,我该如何实施?

如果你有一个规则的网格,只为 x 和 y 坐标提供一维数组就足够了。这样计算成本较低,但我不知道这是否是一般网格情况下出现错误消息的原因。


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:溢出错误