How to make scipy.interpolate give a an extrapolated result beyond the input range?

Posted by Salim Fadhley on Stack Overflow See other posts from Stack Overflow or by Salim Fadhley
Published on 2010-04-30T14:46:56Z Indexed on 2010/04/30 15:07 UTC
Read the original article Hit count: 267

Filed under:
|
|
|

I'm trying to port a program which uses a hand-rolled interpolator (developed by a mathematitian 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.

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 pouints to infinity.

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

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy