我想推断一个函数拟合。
scipy.interpolate.interp1d应该能够做到这一点(请参阅doc片段)。
相反,我得到“ValueError:x_new中的值低于插值范围。”

使用:python 2.7.12,numpy 1.13.3,scipy 0.19.1


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

最佳答案

根据documentation,除了interp1d或指定ValueError之外,fill_value='extrapolate'默认在外推时提高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)

10-04 15:05