0

I have interpolation function:

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)

when i evaluate f(38503) output is 0.75 which is nothing like y_points.

Any suggestion on how to decrease this error using this or other interpolation methods?

Salman Shaukat
  • 333
  • 3
  • 14
  • 1
    That's not interpolation, it's extrapolation (the point you are asking for is outside of bounds of the training set). What "error" are you trying t decrease? This is a perfectly reasonable curve-fitting result if you consider the curve presented by the 3 input points. – RishiG May 14 '18 at 14:29
  • 1
    fyi https://stackoverflow.com/questions/2745329 – Jonas May 15 '18 at 08:54

1 Answers1

1

As RishiG pointed out in the comments, what you want to do is extrapolation.

The object oriented approach has an extra parameter for this: 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))

Output:

0.7591400146484374
0.249267578125

Edit:

This similar question might also be interesting.

Jonas
  • 1,838
  • 4
  • 19
  • 35