我想外推函數擬合。 scipy.interpolate.interp1d應該可以做到這一點(請參閱doc片段)。 相反,我得到「ValueError:x_new中的值低於插值範圍。」scipy interp1d外插方法fill_value =元組不工作
使用:蟒蛇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 forx_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 asbelow, 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
謝謝,克雷格。我認爲給fill_value提供一個元組值會導致它被使用(可能除非你由於某種原因明確地設置了bounds_error = False)。顯然這不是interp1d的行爲。 –