使用LinearNDInterpolator进行外推

时间:2013-12-11 10:34:36

标签: python numpy scipy interpolation extrapolation

我有一个3D数据集,我想插值并线性推断。使用scipy.interpolate.LinearNDInterpolator可以轻松完成插值。模块只能在参数范围之外的值中填充常量/ nan,但我不明白为什么它不会提供打开外推的选项。

查看代码,我看到该模块是用cython编写的。由于没有cython的经验,很难用代码来实现外推。我可以用纯Python代码编写它,但也许这里的其他人有更好的主意?我的特殊情况涉及一个恒定的xy网格,但是z值不断变化很多(-100,000)因此插值必须很快,因为每次z值改变时都会运行插值。

根据要求提供一个基本示例,假设我有一个像

这样的网格
xyPairs = [[-1.0, 0.0], [-1.0, 4.0],
           [-0.5, 0.0], [-0.5, 4.0],
           [-0.3, 0.0], [-0.3, 4.0],
           [+0.0, 0.0], [+0.0, 4.0],
           [+0.2, 0.0], [+0.2, 4.0]]

我们想要计算x = -1.5, -0.8, +0.5y = -0.2, +0.2, +0.5的值。目前,我正在沿着x轴对每个y值执行1d内插/外推,然后沿着y轴对每个x值执行1d内插/外推。外推由ryggyr's answer中的第二个函数完成。

3 个答案:

答案 0 :(得分:3)

我提出了一种方法,代码很糟糕,但我希望它会对你有所帮助。我们的想法是,如果您事先知道要进行外推的界限,可以在数组边缘添加额外的列/行,使用线性外推值,然后在新数组上进行插值。这是一个示例,其中一些数据将被外推,直到x = + - 50和y = + - 40:

import numpy as np
x,y=np.meshgrid(np.linspace(0,6,7),np.linspace(0,8,9)) # create x,y grid
z=x**2*y # and z values
# create larger versions with two more columns/rows
xlarge=np.zeros((x.shape[0]+2,x.shape[1]+2))
ylarge=np.zeros((x.shape[0]+2,x.shape[1]+2))
zlarge=np.zeros((x.shape[0]+2,x.shape[1]+2))
xlarge[1:-1,1:-1]=x # copy data on centre
ylarge[1:-1,1:-1]=y
zlarge[1:-1,1:-1]=z
# fill extra columns/rows
xmin,xmax=-50,50
ymin,ymax=-40,40
xlarge[:,0]=xmin;xlarge[:,-1]=xmax # fill first/last column
xlarge[0,:]=xlarge[1,:];xlarge[-1,:]=xlarge[-2,:] # copy first/last row
ylarge[0,:]=ymin;ylarge[-1,:]=ymax
ylarge[:,0]=ylarge[:,1];ylarge[:,-1]=ylarge[:,-2]
# for speed gain: store factor of first/last column/row
first_column_factor=(xlarge[:,0]-xlarge[:,1])/(xlarge[:,1]-xlarge[:,2]) 
last_column_factor=(xlarge[:,-1]-xlarge[:,-2])/(xlarge[:,-2]-xlarge[:,-3])
first_row_factor=(ylarge[0,:]-ylarge[1,:])/(ylarge[1,:]-ylarge[2,:])
last_row_factor=(ylarge[-1,:]-ylarge[-2,:])/(ylarge[-2,:]-ylarge[-3,:])
# extrapolate z; this operation only needs to be repeated when zlarge[1:-1,1:-1] is updated
zlarge[:,0]=zlarge[:,1]+first_column_factor*(zlarge[:,1]-zlarge[:,2]) # extrapolate first column
zlarge[:,-1]=zlarge[:,-2]+last_column_factor*(zlarge[:,-2]-zlarge[:,-3]) # extrapolate last column
zlarge[0,:]=zlarge[1,:]+first_row_factor*(zlarge[1,:]-zlarge[2,:]) # extrapolate first row
zlarge[-1,:]=zlarge[-2,:]+last_row_factor*(zlarge[-2,:]-zlarge[-3,:]) #extrapolate last row

然后你可以插入(xlarge,ylarge,zlarge)。由于所有操作都是numpy slice操作,我希望它对你来说足够快。更新z数据后,将其复制到zlarge[1:-1,1:-1]并重新执行最后4行。

答案 1 :(得分:3)

使用最近和线性插值的组合。 如果无法插值,LinearNDInterpolator将返回np.nan 否则它返回一个数组大小(1) NearestNDInterpolator返回一个浮点数

import scipy.interpolate
import numpy
class LinearNDInterpolatorExt(object):
  def __init__(self, points,values):
    self.funcinterp=scipy.interpolate.LinearNDInterpolator(points,values)
    self.funcnearest=scipy.interpolate.NearestNDInterpolator(points,values)
  def __call__(self,*args):
    t=self.funcinterp(*args)
    if not numpy.isnan(t):
      return t.item(0)
    else:
      return self.funcnearest(*args)

答案 2 :(得分:0)

我稍微修改了@Keith Williams的答案,对我来说效果很好(注意它不会线性外推-它仅使用最近的邻居):

import numpy as np
from scipy.interpolate import LinearNDInterpolator as linterp
from scipy.interpolate import NearestNDInterpolator as nearest

class LinearNDInterpolatorExt(object):
    def __init__(self, points, values):
        self.funcinterp = linterp(points, values)
        self.funcnearest = nearest(points, values)
    
    def __call__(self, *args):
        z = self.funcinterp(*args)
        chk = np.isnan(z)
        if chk.any():
            return np.where(chk, self.funcnearest(*args), z)
        else:
            return z