在 scipy.interpolate.interp1d 中将元组传递给 fill_value 导致 ValueError

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

scipy.interpolate.interp1d (v0.17.0) 中的 docs 对可选的 fill_value 参数说以下内容:

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].

因此我在这段代码中传递了一个二元元组:

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))

但它抛出 ValueError:

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

任何人都可以指出我在这里做错了什么吗? 在此先感谢您的帮助。

这是一个错误,已在当前开发版本中修复:

>>> 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'

话虽这么说,如果您想要的只是具有左侧和右侧填充值的线性插值,您可以使用 np.interp 直接。