【问题标题】:What do the different values of the kind argument mean in scipy.interpolate.interp1d?kind 参数的不同值在 scipy.interpolate.interp1d 中意味着什么?
【发布时间】:2014-12-30 01:49:22
【问题描述】:

SciPy documentation 解释说interp1dkind 参数可以取值‘linear’‘nearest’‘zero’‘slinear’‘quadratic’‘cubic’。最后三个是样条命令,'linear' 是不言自明的。 'nearest''zero' 是做什么的?

【问题讨论】:

    标签: python scipy interpolation


    【解决方案1】:
    • nearest“捕捉”到最近的数据点。
    • zero 是零阶样条。它在任何时候的值都是最后看到的原始值。
    • linear 执行线性插值,slinear 使用第一个 顺序样条。他们使用不同的代码和can produce similar but subtly different results
    • quadratic 使用二阶样条插值。
    • cubic 使用三阶样条插值。

    请注意,k 参数也可以接受指定样条插值顺序的整数。


    import numpy as np
    import matplotlib.pyplot as plt
    import scipy.interpolate as interpolate
    
    np.random.seed(6)
    kinds = ('nearest', 'zero', 'linear', 'slinear', 'quadratic', 'cubic')
    
    N = 10
    x = np.linspace(0, 1, N)
    y = np.random.randint(10, size=(N,))
    
    new_x = np.linspace(0, 1, 28)
    fig, axs = plt.subplots(nrows=len(kinds)+1, sharex=True)
    axs[0].plot(x, y, 'bo-')
    axs[0].set_title('raw')
    for ax, kind in zip(axs[1:], kinds):
        new_y = interpolate.interp1d(x, y, kind=kind)(new_x)
        ax.plot(new_x, new_y, 'ro-')
        ax.set_title(kind)
    
    plt.show()
    

    【讨论】:

    • 线性和线性在某些病理情况下并不总是产生相同的结果,请参阅gist.github.com/stringfellow/8ae4d3f25ca525e75bb79c01fbda4a24
    • @StevePike:这很有趣,但看起来这可能是 Pandas 特有的问题,而不是 scipy。也就是说,当我将您的数据调整为我上面的代码(没有 Pandas)时,interpolate.interp1d 似乎仍然会在 kind='linear'kind='slinear' 时产生相同的结果。
    • 啊哈.. 好收获!我以为熊猫只是通过它...愚蠢!相当混乱..
    • 实际上,scipy 确实不同,请参阅更新的测试gist.github.com/stringfellow/8ae4d3f25ca525e75bb79c01fbda4a24 - pandas 和 scipy 在这里是相同的 - 尽管它们在绘图上看起来相同,但它们是不同的值,这在某些情况下很重要不要假设它们是相同的!
    • @StevePike:感谢您仔细跟踪并进行编辑。
    【解决方案2】:

    ‘nearest’ 返回 X 中离参数最近的数据点,或者 interpolates function y=f(x) at the point x using the data point nearest to x

    'zero' 我猜相当于截断参数,因此使用最接近零的数据点

    【讨论】:

      猜你喜欢
      • 2014-11-26
      • 2020-02-05
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2011-08-27
      • 2011-05-04
      • 2021-04-30
      相关资源
      最近更新 更多