`ValueError: A value in x_new is above the interpolation range.` – what other reasons than not ascending values?

If you are running Scipy v. 0.17.0 or newer, then you can pass fill_value="extrapolate" to spi.interp1d, and it will extrapolate to accomadate these values of your’s that lie outside the interpolation range. So define your interpolation function like so:

intfunc = spi.interp1d(coarsex, coarsey,axis=0, fill_value="extrapolate")

Be forewarned, however!

Depending on what your data looks like and the type on interpolation you are performing, the extrapolated values can be erroneous. This is especially true if you have noisy or non-monotonic data. In your case you might be ok because your x_new value is only slighly beyond your interpolation range.

Here’s simple demonstration of how this feature can work nicely but also give erroneous results.

import scipy.interpolate as spi
import numpy as np

x = np.linspace(0,1,100)
y = x + np.random.randint(-1,1,100)/100
x_new = np.linspace(0,1.1,100)
intfunc = spi.interp1d(x,y,fill_value="extrapolate")
y_interp = intfunc(x_new)

import matplotlib.pyplot as plt
plt.plot(x_new,y_interp,'r', label="interp/extrap")
plt.plot(x,y, 'b--', label="data")
plt.legend()
plt.show()

enter image description here

So the interpolated portion (in red) worked well, but the extrapolated portion clearly fails to follow the otherwise linear trend in this data because of the noise. So have some understanding of your data and proceed with caution.

Leave a Comment