给定分散输入数据构建二维插值器

Constructing a 2d interpolator given scattered input data

我有如下三个列表:

x = [100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0]

y = [300.0, 300.0, 300.0, 300.0, 500.0, 500.0, 500.0, 500.0, 700.0, 700.0, 700.0, 700.0, 1000.0, 1000.0, 1000.0, 1000.0, 1500.0, 1500.0, 1500.0, 1500.0, 2000.0, 2000.0, 2000.0, 2000.0, 3000.0, 3000.0, 3000.0, 3000.0, 5000.0, 5000.0, 5000.0, 5000.0, 7500.0, 7500.0, 7500.0, 75000.0, 10000.0, 10000.0, 10000.0, 10000.0]

z = [100.0, 95.0, 87.5, 77.5, 60.0, 57.0, 52.5, 46.5, 40.0, 38.0, 35.0, 31.0, 30.0, 28.5, 26.25, 23.25, 23.0, 21.85, 20.125, 17.825, 17.0, 16.15, 14.875, 13.175, 13.0, 12.35, 11.375, 10.075, 10.0, 9.5, 8.75, 7.75, 7.0, 6.65, 6.125, 5.425, 5.0, 4.75, 4.375, 3.875]

每个列表的每个条目都被读取为一个点,所以点 0 是 (100,300,100) 点 1 是 (75,300,95) 等等。

我正在尝试进行 2d 插值,以便我可以计算任何给定输入 (x0, y0) 点的 z 值。

我读到过使用 meshgrid 我可以用 scipy 中的 RegularGridInterpolator 进行插值,但我不确定如何设置它:

x_,y_,z_ = np.meshgrid(x,y,z) # both indexing ij or xy

我没有得到 x_,y_,z_ 有意义的值,我不确定如何从那里开始。

我正在尝试使用上面的数据点来查找中间值,因此类似于 scipy 的 interp1d,其中

f = interp1d(x, y, kind='cubic')

稍后我可以调用 f(any (x,y) point within range) 并获得相应的 z 值。

您需要对分散数据进行二维插值。在这种情况下,我默认使用 scipy.interpolate.griddata,但您似乎想要一个 callable 插值器,而 griddata 需要一组给定的点插值。

不用担心:griddata 二维三次插值使用 CloughTocher2DInterpolator。所以我们完全可以这样做:

import numpy as np
import scipy.interpolate as interp

x = [100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0, 100.0, 75.0, 50.0, 0.0]
y = [300.0, 300.0, 300.0, 300.0, 500.0, 500.0, 500.0, 500.0, 700.0, 700.0, 700.0, 700.0, 1000.0, 1000.0, 1000.0, 1000.0, 1500.0, 1500.0, 1500.0, 1500.0, 2000.0, 2000.0, 2000.0, 2000.0, 3000.0, 3000.0, 3000.0, 3000.0, 5000.0, 5000.0, 5000.0, 5000.0, 7500.0, 7500.0, 7500.0, 75000.0, 10000.0, 10000.0, 10000.0, 10000.0]
z = [100.0, 95.0, 87.5, 77.5, 60.0, 57.0, 52.5, 46.5, 40.0, 38.0, 35.0, 31.0, 30.0, 28.5, 26.25, 23.25, 23.0, 21.85, 20.125, 17.825, 17.0, 16.15, 14.875, 13.175, 13.0, 12.35, 11.375, 10.075, 10.0, 9.5, 8.75, 7.75, 7.0, 6.65, 6.125, 5.425, 5.0, 4.75, 4.375, 3.875]

interpolator = interp.CloughTocher2DInterpolator(np.array([x,y]).T, z)

现在你可以用2个坐标调用这个插值器来给你相应的插值数据点:

>>> interpolator(x[10], y[10]) == z[10]
True
>>> interpolator(2, 300)
array(77.81343)

请注意,您必须留在输入点的凸包内,否则您将得到 nan(或作为 fill_value 关键字传递给插值器的任何内容):

>>> interpolator(2, 30)
array(nan)

无论如何,外推通常是没有意义的,而且您的输入点分散得有点不稳定:

所以即使外推是可能的我也不会相信。


只是为了演示生成的插值器如何被限制到输入点的凸包,这是我们为绘图而创建的网格化网格上的数据表面图:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# go linearly in the x grid
xline = np.linspace(min(x), max(x), 30)
# go logarithmically in the y grid (considering y distribution)
yline = np.logspace(np.log10(min(y)), np.log10(max(y)), 30)
# construct 2d grid from these
xgrid,ygrid = np.meshgrid(xline, yline)
# interpolate z data; same shape as xgrid and ygrid
z_interp = interpolator(xgrid, ygrid)

# create 3d Axes and plot surface and base points
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(xgrid, ygrid, z_interp, cmap='viridis',
                vmin=min(z), vmax=max(z))
ax.plot(x, y, z, 'ro')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

这是两个角度的输出(最好交互旋转;这样的静止图像不符合 3d 表示法):

有两个主要特点需要注意:

  1. 表面与红点非常吻合,这是插值所期望的。幸运的是,输入点很好很平滑,所以插值一切顺利。 (红点通常被表面隐藏的事实只是由于 pyplot 的渲染器如何错误处理复杂 3d 对象的相对位置)
  2. 表面沿着输入点的凸包被切割(由于 nan 值),所以即使我们的网格化数组定义了一个矩形网格,我们也只能在插值有意义的地方切割表面.