Search code examples
pythonscipyinterpolationspline

Scipy spline interpolation outside data points region


I am fitting 2d points on a 1d axis, using scipy.interpolate.bisplrep. This works better than the polynomial interpolation in general, but outside the points used for fitting, it gives bounded values. I can't find anything like fill_value="extrapolate", that we can use with interp1d.

As splines are piecewise polynomials, I thought I could use the coefficients of the closest polynomial, but I don't know how.

xy = [[0, 0], [0, 1], [1, 0], [1, 1], [2, 2], [0, 2], [2, 0], [2, 1], [0.1, 0]]
x, y = tuple(list(zip(*xy)))
z =  [0, 1, 1, 1.1, 3, 2, 2, 3, 0.1]
spline = interpolate.bisplrep(x, y, z, kx=2, ky=2)

print(interpolate.bisplev([0], [0], spline)) # close to 0 as expected
print(interpolate.bisplev([1], [1], spline)) # 1.1 as expected
print(interpolate.bisplev([2], [2], spline)) # 3 as expected
print(interpolate.bisplev([3], [3], spline)) # 3 but should return way more

Solution

  • What @jared said in the comments: bisplrep/bisplev does not support extrapolation: https://github.com/scipy/scipy/issues/3361

    That said, if you do not need smoothing, RegularGridInterpolator does extrapolate.