代码之家  ›  专栏  ›  技术社区  ›  Ethunxxx

使用scipy.interpolate.Rbf()进行不准确的插值

  •  3
  • Ethunxxx  · 技术社区  · 7 年前

    当我执行以下代码时

    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,相应的绘图如下:

    Plot of the array 'data_fine'

    但是,我希望在这样一个简单的插值情况下,插值值会更接近正确的值,即1.000。

    我认为插值值的变化是由插值点到给定数据点的距离不同引起的。

    我的问题是:有没有办法避免这种行为?我怎样才能得到一个插值,它不是由插值点到数据点的距离加权的,只给我1.000英寸 插值数据 ?

    1 回复  |  直到 7 年前
        1
  •  3
  •   user6655984user6655984    7 年前

    在这种简单的插值情况下,

    毫无根据的期望。径向基函数插值,顾名思义,使用径向基函数。默认情况下,基函数 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 
    

    但是这不是一个好主意,因为当数据 常数时,会有太多的长程影响。

    只给我1000个插值数据?

    那就是线性插值。 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 有更多的插值模式。