当我执行下面的代码时
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import Rbf
x_coarse, y_coarse = np.mgrid[0:5, 0:5]
x_fine, y_fine = np.mgrid[1:4:0.23,1:4:0.23]
data_coarse = np.ones([5,5])
rbfi = Rbf(x_coarse.ravel(), y_coarse.ravel(), data_coarse.ravel())
interpolated_data = rbfi(x_fine.ravel(), y_fine.ravel()).reshape([x_fine.shape[0],
y_fine.shape[0]])
plt.imshow(interpolated_data)
数组 interpolated_data
的值介于 0.988 到 1.002 之间,对应的绘图如下所示:
但是,我希望在这种简单的插值情况下,插值会更接近正确值,即 1.000。
我认为插值的变化是由插值点到给定数据点的不同距离引起的。
我的问题是:有没有办法避免这种行为?我怎样才能得到一个不由插值点到数据点的距离加权的插值,并且在 interpolated_data
中只给我 1.000?
最佳答案
I would expect that in such a simple interpolation case,
毫无根据的期望。 RBF 插值,顾名思义,使用径向基函数。默认情况下,基函数 sqrt((r/epsilon)**2 + 1)
其中 r 是距数据点的距离,epsilon 是正参数。这些函数的加权和不可能是相同的常数。 RBF 插值不同于线性或双线性插值。这是一种适用于粗糙数据的粗略插值。
通过设置大得离谱的 epsilon,您可以更接近 1;只是因为它使网格上的基函数几乎相同。
rbfi = Rbf(x_coarse.ravel(), y_coarse.ravel(), data_coarse.ravel(), epsilon=10)
# ...
print(interpolated_data.min(), interpolated_data.max())
# outputs 0.9999983458255883 1.0000002402521204
但这不是一个好主意,因为当数据不恒定时,插值中会有太多的远程影响。
gives me nothing but 1.000 in interpolated_data?
那将是线性插值。 LinearNDInterpolator
具有与 Rbf
相似的语法,因为它返回一个可调用对象。
linear = LinearNDInterpolator(np.stack((x_coarse.ravel(), y_coarse.ravel()), axis=-1),
data_coarse.ravel())
interpolated_data = linear(x_fine.ravel(), y_fine.ravel()).reshape([x_fine.shape[0], y_fine.shape[0]])
print(interpolated_data.min(), interpolated_data.max())
# outputs 1.0 1.0
还有一个griddata
它有更多的插值模式。
关于python - scipy.interpolate.Rbf() 的插值不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51701844/