2016-05-04 29 views
2

docs在scipy.interpolate.interp1d(v0.17.0)說用於可選fill_value參數如下:傳遞的元組在scipy.interpolate.interp1d結果fill_value在ValueError異常

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

因此我通過在這兩個代碼元素TUPE:

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) 

任何人都可以請指出我在這裏做錯了什麼? 在此先感謝您的幫助。

回答

4

這已固定在當前開發版中的錯誤:

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

+0

非常感謝,我不知道np.interp – jmborr