Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scipy interp1d extrapolation method fill_value = tuple not working

I want to extrapolate a function fit. scipy.interpolate.interp1d is supposed to be able to do this (see doc snippet). Instead I get "ValueError: A value in x_new is below the interpolation range."

Using: python 2.7.12, numpy 1.13.3, scipy 0.19.1

fill_value : array-like or (array-like, array_like) or "extrapolate", optional - if a ndarray (or float), this value will be used to fill in for requested points outside of the data range. If not provided, then the default is NaN. The array-like must broadcast properly to the dimensions of the non-interpolation axes. - 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]. Anything that is not a 2-element tuple (e.g., list or ndarray, regardless of shape) is taken to be a single array-like argument meant to be used for both bounds as below, above = fill_value, fill_value.

import numpy as np
from scipy.interpolate import interp1d
# make a time series
nobs = 10
t = np.sort(np.random.random(nobs))
x = np.random.random(nobs)
# compute linear interp (with ability to extrapolate too)
f1 = interp1d(t, x, kind='linear', fill_value='extrapolate') # this works
f2 = interp1d(t, x, kind='linear', fill_value=(0.5, 0.6)) # this doesn't
like image 432
John Mahoney Avatar asked Oct 21 '17 00:10

John Mahoney


People also ask

How does Scipy interp1d work?

The interp1d class in the scipy. interpolate is a convenient method to create a function based on fixed data points, which can be evaluated anywhere within the domain defined by the given data using linear interpolation. By using the above data, let us create a interpolate function and draw a new interpolated graph.

How does interp1d work in Python?

Interpolate a 1-D function. x and y are arrays of values used to approximate some function f: y = f(x) . This class returns a function whose call method uses interpolation to find the value of new points.


1 Answers

According to the documentation, interp1d defaults to raising ValueError on extrapolation except when fill_value='extrapolate' or when you specify bounds_error=False.

In [1]: f1 = interp1d(t, x, kind='linear', fill_value=(0.5, 0.6), bounds_error=False)

In [2]: f1(0)
Out[2]: array(0.5)
like image 182
Craig Avatar answered Nov 14 '22 22:11

Craig