Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing a tuple to fill_value in scipy.interpolate.interp1d results in ValueError

The docs in scipy.interpolate.interp1d (v0.17.0) say the following for the optional fill_value argument:

fill_value : ... If a two-element tuple, then the first element is used as a fill value for x_new < x[0] and the second element is used for x_new x[-1].

Thus I pass a two-element tupe in this code:

N=100
x=numpy.arange(N)
y=x*x
interpolator=interp1d(x,y,kind='linear',bounds_error=False,fill_value=(x[0],x[-1]))
r=np.arange(1,70)
interpolator(np.arange(1,70))

But it throws ValueError:

ValueError: shape mismatch: value array of shape (2,) could not be broadcast to indexing result of shape (0,1)

Can anyone please point to me what am I doing wrong here? Thanks in advance for any help.

like image 479
jmborr Avatar asked May 04 '16 22:05

jmborr


People also ask

What does Scipy interpolate interp1d return?

This class returns a function whose call method uses interpolation to find the value of new points. A 1-D array of monotonically increasing real values. A N-D array of real values.

What is interp1d in Python?

The interp1d() function of scipy. interpolate package is used to interpolate a 1-D function. It takes arrays of values such as x and y to approximate some function y = f(x) and then uses interpolation to find the value of new points.

What does scipy interpolate do?

The scipy. interpolate. Rbf is used for interpolating scattered data in n-dimensions. The radial basis function is defined as corresponding to a fixed reference data point.


1 Answers

It's a bug which has been fixed in the current dev version:

>>> N = 100
>>> x = np.arange(N)
>>> y = x**2
>>> from scipy.interpolate import interp1d
>>> iii = interp1d(x, y, fill_value=(-10, 10), bounds_error=False)
>>> iii(-1)
array(-10.0)
>>> iii(101)
array(10.0)
>>> scipy.__version__
'0.18.0.dev0+8b07439'

That being said, if all you want is a linear interpolation with fill values for left-hand and right-hand sides, you can use np.interp directly.

like image 123
ev-br Avatar answered Sep 25 '22 09:09

ev-br