英文:
SciPy LinearNDInterpolator RegularGridInterpolator produces different result for the same data
问题
我正在尝试使用SciPy中的LinearNDInterpolator
和RegularGridInterpolator
函数进行线性插值1D、2D和3D数据。
我理解这两种方法之间的主要区别在于,RegularGridInterpolator
被设计用于处理在矩形(或“规则”)网格上具有均匀或不均匀间距的数据。与LinearNDInterpolator
相比,这个函数也更高效。
然而,当我在具有均匀间距的矩形网格上生成随机数据时,从这两种方法得到的结果不匹配。我原以为它们会相似,因为网格是规则的,我在两种情况下都使用了线性插值。
以下是我用来比较这两种方法的代码:
import numpy as np
from scipy.interpolate import LinearNDInterpolator
from scipy.interpolate import RegularGridInterpolator
import matplotlib.pyplot as plt
#原始数据
x = np.linspace(0, 1, num=20)
y = np.linspace(1, 2, num=10)
X, Y = np.meshgrid(x, y)
values = np.random.rand(20, 10)
points = np.column_stack((X.flatten(), Y.flatten()))
values_flat = values.flatten()
#LinearNDInterpolation
interfunc = LinearNDInterpolator(points, values_flat)
x1 = np.linspace(0, 1, num=3000)
y1 = np.linspace(1, 2, num=3000)
X1, Y1 = np.meshgrid(x1, y1)
interpolated_values = interfunc(np.column_stack((X1.flatten(), Y1.flatten())))
interpolated_values = interpolated_values.reshape(X1.shape)
fig, ax = plt.subplots()
linear = ax.contourf(X1, Y1, interpolated_values.T)
fig.colorbar(linear, ax=ax)
#RegularGridInterpolation
fig2, ax2 = plt.subplots()
x2 = np.linspace(0, 1, num=3000)
y2 = np.linspace(1, 2, num=3000)
X2, Y2 = np.meshgrid(x2, y2)
points_grid = (x, y)
interfunc_grid = RegularGridInterpolator(points_grid, values, method="linear")
interpolated_values_grid = interfunc_grid(np.column_stack((X2.flatten(), Y2.flatten())))
interpolated_values_grid = interpolated_values_grid.reshape(X2.shape)
d = ax2.contourf(X2, Y2, interpolated_values_grid.T)
fig2.colorbar(d, ax=ax2)
plt.show()
这段代码生成了两个轮廓图:一个用于LinearNDInterpolator
,另一个用于RegularGridInterpolator
。这些图显示了在更精细的网格上的插值值。
在我的代码中是否犯了错误,或者这两种插值方法的结果之间有其他原因不匹配?任何见解都将不胜感激。
英文:
I am experimenting with the LinearNDInterpolator
and RegularGridInterpolator
functions from SciPy to interpolate 1D, 2D and 3D data linearly.
I understand that the main difference between the two methods is that the RegularGridInterpolator
fis designed to handle data on a rectilinear (or "regular") grid with either even or uneven spacing. This function is also more efficient compared to LinearNDInterpolator
.
However, when I generate random data on a rectilinear grid with even spacings, the results obtained from these two methods don't match. I was expecting them to be similar since the grid is regular and I'm using linear interpolation in both cases.
Here is the code I used to compare these two methods:
import numpy as np
from scipy.interpolate import LinearNDInterpolator
from scipy.interpolate import RegularGridInterpolator
import matplotlib.pyplot as plt
#original data
x = np.linspace(0, 1, num=20)
y = np.linspace(1, 2, num=10)
X, Y = np.meshgrid(x, y)
values = np.random.rand(20, 10)
points = np.column_stack((X.flatten(), Y.flatten()))
values_flat = values.flatten()
#LinearNDInterpolation
interfunc = LinearNDInterpolator(points, values_flat)
x1 = np.linspace(0, 1, num=3000)
y1 = np.linspace(1, 2, num=3000)
X1, Y1 = np.meshgrid(x1, y1)
interpolated_values = interfunc(np.column_stack((X1.flatten(), Y1.flatten())))
interpolated_values = interpolated_values.reshape(X1.shape)
fig, ax = plt.subplots()
linear = ax.contourf(X1, Y1, interpolated_values.T)
fig.colorbar(linear, ax=ax)
#RegularGridInterpolation
fig2, ax2 = plt.subplots()
x2 = np.linspace(0, 1, num=3000)
y2 = np.linspace(1, 2, num=3000)
X2, Y2 = np.meshgrid(x2, y2)
points_grid = (x, y)
interfunc_grid = RegularGridInterpolator(points_grid, values, method="linear")
interpolated_values_grid = interfunc_grid(np.column_stack((X2.flatten(), Y2.flatten())))
interpolated_values_grid = interpolated_values_grid.reshape(X2.shape)
d = ax2.contourf(X2, Y2, interpolated_values_grid.T)
fig2.colorbar(d, ax=ax2)
plt.show()
The code generates two contour plots: one for the LinearNDInterpolator
and the other for the RegularGridInterpolator
. The plots show the interpolated values over a finer grid.
Am I making a mistake somewhere in my code or is there another reason why the results from these two methods of interpolation would differ? Any insights would be greatly appreciated.
答案1
得分: 1
LinearNDInterpolator基于三角剖分(通过QHull实现)。每个插值值都是恰好三个点的线性组合。而对于网格上的数据,选择这三个点是退化的。
简而言之:不要在网格数据上使用LinearNDInterpolator。
英文:
LinearNDIbterpolator is based on triangulation (via QHull). Each interpolated value is a linear combination of exactly three points. And the choice of these three is degenerate for data on a grid.
Bottom line : do not use LinearNDinterpolator for data on a grid.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论