索引错误与scipy.interpolate.interp1d



我正在尝试使用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 result = 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 号线,在斯普勒瓦尔 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() 

Elyase是对的。我切换到计算机上的Enthought python发行版 - 通过包含"#!/usr/bin/env python",这让它工作了。一定是我使用的 scipy 版本有问题。感谢您的快速建议!

最新更新