簡體   English   中英

scipy.interpolate.interp1d的索引錯誤

[英]Index error with scipy.interpolate.interp1d

我正在嘗試使用scipy.interpolate.interp1d函數處理三次樣條。 我試圖使文檔頁面上的示例正常工作,但是每當我運行它時,都會出現此錯誤:

plt.plot(x,y,'o',xnew,f(xnew),'-',xnew,f2(xnew),'-')File“ /Library/Python/2.7/site-packages/scipy- 0.12.0.dev_ddd617d_20120920-py2.7-macosx-10.8-x86_64.egg / scipy / interpolate / interpolate.py“,第396行, 調用 y_new = self._call(x_new)文件“ /Library/Python/2.7/site -packages / scipy-0.12.0.dev_ddd617d_20120920-py2.7-macosx-10.8-x86_64.egg / scipy / interpolate / interpolate.py“,第372行,位於_call_spline結果= spleval(self._spline,x_new.ravel() )文件“ /Library/Python/2.7/site-packages/scipy-0.12.0.dev_ddd617d_20120920-py2.7-macosx-10.8-x86_64.egg/scipy/interpolate/interpolate.py”,行835,格式為[ sl] = _fitpack._bspleval(xx,xj,cvals [sl],k,deriv)IndexError:索引過多

因此,它適用於線性插值,但不適用於三次插值。 我可能會犯一些愚蠢的錯誤,但我無法弄清楚出了什么問題。 這是我正在使用的示例的代碼:

import numpy as np
from scipy.interpolate import interp1d

x = np.linspace(0, 10, 40)
y = np.cos(-x**2/8.0)
f = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')

xnew = np.linspace(0, 10, 10)
import matplotlib.pyplot as plt
plt.plot(x,y,'o',xnew,f(xnew),'-', xnew, f2(xnew),'--')
plt.legend(['data', 'linear', 'cubic'], loc='best')
plt.show() 

Elyase是正確的。 我切換到計算機上的Enthought python發行版-通過在腳本頂部添加“#!/ usr / bin / env python”,使它可以正常工作。 我使用的scipy版本一定有問題。 感謝您的快速建議!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM