本文介绍了scipy中二次和二阶样条插值之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写使用scipy.interpolate函数在python中计算一维插值的函数.借助文档的帮助,我为立方和三次样条插值编写了2个不同的函数

I am writing functions that will calculate 1d interpolations in python using scipy.interpolate function. using help from documentation I wrote 2 different functions for cubic and cubic spline interpolation

# calculate cubic interpolation
def linear_interpolation(x):
    linear = interpolate.interp1d(support_x, support_y, 'cubic')
    return linear(x)

# calculate cubic spline interpolation
def cubic_spline_interpolation(x):
    tck = interpolate.splrep(support_x, support_y)
        return interpolate.splev(x, tck)

我对这里的方法有些困惑.如果我使用interpolate.interp1d(support_x, support_y, 'cubic'),与cubic spline方法有什么不同吗? kind = 'quadratic'second order spline有什么区别?

I am a bit confused about the methods here. If I use interpolate.interp1d(support_x, support_y, 'cubic') , is that different from cubic spline method? Also what is the difference between kind = 'quadratic' and second order spline?

文档说,(线性",最近",零",线性",二次方",三次方",其中线性",二次方"和三次方"是指第一个样条插值,二阶或三阶),那么为什么我必须为三次样条曲线编写不同的函数,而不仅仅是将其更改为kind=cubic

the documentation says, (‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic, ‘cubic’ where ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of first, second or third order), so why do I have to write different function for cubic spline instead of just changing it to kind=cubic

推荐答案

它们都返回相同的样条,尽管在内部实现不相同(与splrep几乎是所有Fortran代码). 二次方"的含义与2度相同,三次方"的含义与3度相同.区别:

They both return the same splines, although internally, the implementation is not the same (interp1d is more recent and has greater Python code percentage, compared to splrep which is nearly all Fortran code). "Quadratic" means the same as 2nd degree, and "cubic" is 3rd degree. Some distinction:

  • splrep及其近亲 UnivariateSpline 是功能更丰富的样条线构建例程;它们允许使用创建非插值样条曲线的平滑参数.
  • 如果不需要平滑处理,
  • interp1d可能更易于使用.
  • splrep and its close relative UnivariateSpline are more feature-rich spline construction routines; they allow for a smoothing parameter which creates non-interpolating spline.
  • interp1d may be simpler to use if you do not require smoothing.

无论如何,这远非SciPy中唯一的冗余功能实例.添加了新方法和参数,但保留了旧方法和参数以实现向后兼容.

In any event, this is far from the only instance of redundant functionality in SciPy. New methods and parameters are added, but old ones are kept for backward compatibility.

历史记录:在较早版本的SciPy(例如0.15.1)中,interp1d返回的样条线与splrep相比质量较低(此答案的第一版基于0.15.1) .在当前版本0.19.1中,不再存在此问题:两者都返回相同的样条曲线.这是一个演示:

Historical note: in older versions of SciPy (e.g., 0.15.1), interp1d returned rather different splines, of lower quality compared to splrep (the first revision of this answer was based on version 0.15.1). In the current version 0.19.1 this issue is no longer present: both return the same spline. Here is a demonstration:

import numpy as np
from scipy.interpolate import interp1d, splrep, splev

x = np.linspace(0, 6, 7)
y = np.array([3, 1, 4, 1, 5, 5, 2])    # some data
xx = np.linspace(0, 6, 100)            # evaluation points

y1 = interp1d(x, y, kind='cubic')(xx)
y2 = splev(xx, splrep(x, y, k=3))
print(np.abs(y1-y2).max())

y1 = interp1d(x, y, kind='quadratic')(xx)
y2 = splev(xx, splrep(x, y, k=2))
print(np.abs(y1-y2).max())

输出显示这两个例程在典型的数字误差范围内一致.

Output shows that both routines agree within typical numerical errors.

2.6645352591e-15
1.7763568394e-15

这篇关于scipy中二次和二阶样条插值之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 22:52