2017-10-21 10 views
1

Je veux extrapoler un ajustement de fonction. scipy.interpolate.interp1d est censé être en mesure de le faire (voir extrait de doc). Au lieu de cela, j'ai "ValueError: une valeur dans x_new est en dessous de la plage d'interpolation."méthode d'extrapolation interp1d scipy fill_value = tuple ne fonctionne pas

utilisant: 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 

Répondre

0

Selon les documentation, interp1d par défaut élever ValueError sur l'extrapolation, sauf si fill_value='extrapolate' ou lorsque vous spécifiez 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) 
+0

Merci, Craig. J'ai supposé que fournir une valeur de tuple à fill_value provoquerait son utilisation (peut-être à moins que vous ne définissiez explicitement bounds_error = False pour une raison quelconque). Apparemment, ce n'est pas le comportement de interp1d. –