如何使scipy.interpolate给出超出输入范围的推断结果? [英] How to make scipy.interpolate give an extrapolated result beyond the input range?

查看:296
本文介绍了如何使scipy.interpolate给出超出输入范围的推断结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试移植一个程序,该程序使用手摇插值器(由数学家colleage开发)来使用scipy提供的插值器.我想使用或包装scipy插值器,以使其行为与旧的插值器尽可能接近.

I'm trying to port a program which uses a hand-rolled interpolator (developed by a mathematician colleage) over to use the interpolators provided by scipy. I'd like to use or wrap the scipy interpolator so that it has as close as possible behavior to the old interpolator.

这两个函数之间的关键区别在于,在我们的原始插值器中-如果输入值大于或小于输入范围,则我们的原始插值器将推断结果.如果您使用scipy插值器尝试此操作,则会引发ValueError.以该程序为例:

A key difference between the two functions is that in our original interpolator - if the input value is above or below the input range, our original interpolator will extrapolate the result. If you try this with the scipy interpolator it raises a ValueError. Consider this program as an example:

import numpy as np
from scipy import interpolate

x = np.arange(0,10)
y = np.exp(-x/3.0)
f = interpolate.interp1d(x, y)

print f(9)
print f(11) # Causes ValueError, because it's greater than max(x)

有没有一种明智的方法可以使最后一行不会发生崩溃,而是简单地进行线性外推,将由前两个点定义的渐变继续到无穷大.

Is there a sensible way to make it so that instead of crashing, the final line will simply do a linear extrapolate, continuing the gradients defined by the first and last two points to infinity.

请注意,在实际的软件中,我实际上并没有使用exp函数-此处仅用于说明!

Note, that in the real software I'm not actually using the exp function - that's here for illustration only!

推荐答案

1.常数外推

您可以从scipy使用interp函数,它将左右值作为超出范围的常数外推:

1. Constant extrapolation

You can use interp function from scipy, it extrapolates left and right values as constant beyond the range:

>>> from scipy import interp, arange, exp
>>> x = arange(0,10)
>>> y = exp(-x/3.0)
>>> interp([9,10], x, y)
array([ 0.04978707,  0.04978707])

2.线性(或其他自定义)推断

您可以在插值函数周围编写包装器,该函数负责线性插值.例如:

2. Linear (or other custom) extrapolation

You can write a wrapper around an interpolation function which takes care of linear extrapolation. For example:

from scipy.interpolate import interp1d
from scipy import arange, array, exp

def extrap1d(interpolator):
    xs = interpolator.x
    ys = interpolator.y

    def pointwise(x):
        if x < xs[0]:
            return ys[0]+(x-xs[0])*(ys[1]-ys[0])/(xs[1]-xs[0])
        elif x > xs[-1]:
            return ys[-1]+(x-xs[-1])*(ys[-1]-ys[-2])/(xs[-1]-xs[-2])
        else:
            return interpolator(x)

    def ufunclike(xs):
        return array(list(map(pointwise, array(xs))))

    return ufunclike

extrap1d具有插值函数,并返回一个也可以外推的函数.您可以像这样使用它:

extrap1d takes an interpolation function and returns a function which can also extrapolate. And you can use it like this:

x = arange(0,10)
y = exp(-x/3.0)
f_i = interp1d(x, y)
f_x = extrap1d(f_i)

print f_x([9,10])

输出:

[ 0.04978707  0.03009069]

这篇关于如何使scipy.interpolate给出超出输入范围的推断结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆