I am trying to get a cubic spline working with the scipy.interpolate.interp1d function. I tried to get the example on the documentation page working, but whenever I run it I get this error:
我正在尝试用一个三次样条来处理这个插值。插值。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", line 396, in call y_new = self._call(x_new) 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", line 372, in _call_spline result = spleval(self._spline,x_new.ravel()) 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", line 835, in spleval res[sl] = _fitpack._bspleval(xx,xj,cvals[sl],k,deriv) IndexError: too many indices
plt.plot(x,y,'o',xnew,f(xnew),'-', xnew, f2(xnew),'- ' . ')文件"/Library/Python/2.7/site-package /scip -0.12.0.dev_ddd617d_20120920-py2.7-macosx-10.8-x86_64.egg/scipy/ add .py",第396行,in call y_new = self._call(x_new)文件"/Library/Python/2.7/site-package /scip -0.12.0.dev_ddd617d_20120920-py2.7-macosx-10.8-x86_64.egg/scipy/插值/插值.py",第372行," _call_spline结果= spleval(self。_spline,x_new.ravel())文件"/Library/Python/2.7/site-package /scip -0.12.0.dev_ddd617d_20120920-py2.7-macosx-10.8-x86_64.egg/scipy/插值/插值.py",第835行,spleval res[sl] = _fitpack._bspleval(xx,xj,cvals[sl],k,deriv) IndexError:太多索引。
So, it works with the linear interpolation but not with the cubic. I'm probably making some silly error, but I can't figure out what's going wrong. Here is the code for the example that I am using:
所以,它与线性插值有关,但不适用于立方。我可能犯了一些愚蠢的错误,但我不知道出了什么问题。下面是我使用的示例代码:
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()
1 个解决方案
#1
0
Elyase was right. I switched to the Enthought python distribution on my computer – by including "#! /usr/bin/env python" at the top of the script and this made it work. Must be some problem with the scipy version that I was using. Thanks for the quick advice!
Elyase是正确的。我切换到我电脑上的Enthought python发行版——包括“#!”在脚本的顶部,这使它起作用。一定是我使用的scipy版本的问题。谢谢你的快速建议!
#1
0
Elyase was right. I switched to the Enthought python distribution on my computer – by including "#! /usr/bin/env python" at the top of the script and this made it work. Must be some problem with the scipy version that I was using. Thanks for the quick advice!
Elyase是正确的。我切换到我电脑上的Enthought python发行版——包括“#!”在脚本的顶部,这使它起作用。一定是我使用的scipy版本的问题。谢谢你的快速建议!