Extrapolate with LinearNDInterpolator

后端 未结 3 1406
长情又很酷
长情又很酷 2021-01-03 13:55

I\'ve got a 3D dataset that I want to interpolate AND extrapolate linearly. The interpolation can be done easily with scipy.interpolate.LinearNDInterpolator. The module can

3条回答
  •  囚心锁ツ
    2021-01-03 14:41

    use combination of nearest and linear interpolation. LinearNDInterpolator returns np.nan if it fails to interpolate otherwise it returns an array size(1) NearestNDInterpolator returns a float

    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)
    

提交回复
热议问题