我正在尝试使用 scipy.interpolate.interp1d 函数获得三次样条。我试图让文档页面上的示例正常工作,但每当我运行它时,我都会收到此错误:
plt.plot(x,y,'o',xnew,f(xnew),'-',xnew,f2(xnew),'--') 文件“/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行,在spleval res[ 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()