【问题标题】:How to set upper and lower bound on B-Spline result to get a reasonable interpolation如何设置 B-Spline 结果的上限和下限以获得合理的插值
【发布时间】:2018-05-14 14:02:01
【问题描述】:

我有插值功能:

from scipy import interpolate
def f(x):
  x_points = [38508,38510,38512]
  y_points = [0.249267578125,0.181396484375,0.1912841796875]

  tck = interpolate.splrep(x_points, y_points,k=2,xb=38508,xe=38512)
  return interpolate.splev(x, tck)

当我评估 f(38503) 时,输出是 0.75,这与 y_points 完全不同。

关于如何使用这种或其他插值方法减少此错误的任何建议?

【问题讨论】:

  • 这不是插值,而是外推(您要求的点超出了训练集的范围)。你想减少什么“错误”?如果考虑 3 个输入点呈现的曲线,这是一个完全合理的曲线拟合结果。

标签: python math interpolation


【解决方案1】:

正如 RishiG 在 cmets 中指出的那样,您要做的是外推。

object oriented approach 有一个额外的参数:ext

from scipy import interpolate

def f(x):
    x_points = [38508, 38510, 38512]
    y_points = [0.249267578125, 0.181396484375, 0.1912841796875]

    tck = interpolate.splrep(x_points, y_points,k=2,xb=38508,xe=38512)
    return interpolate.splev(x, tck)

def g(x):
    x_points = [38508, 38510, 38512]
    y_points = [0.249267578125, 0.181396484375, 0.1912841796875]

    spl = interpolate.UnivariateSpline(x_points, y_points, k=2, ext=3)

    return spl(x)

if __name__=='__main__':

    print(f(38503))
    print(g(38503))

输出:

0.7591400146484374
0.249267578125

编辑:

This similar question 也可能很有趣。

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 2012-01-11
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多