2025年python interpolate.interp1d_索引错误scipy.interpolate.interp1d「建议收藏」

python interpolate.interp1d_索引错误scipy.interpolate.interp1d「建议收藏」我试图得到一个三次样条函数 scipy interpolate interp1d 功能 我试图让 documentatio page 上的示例正常工作 但每当我运行它时 都会出现以下错误 plt plot x y o xnew f xnew xnew f2 xnew File Library Python 2 7 site packages scipy 0 12 0

我试图得到一个三次样条函数scipy.interpolate.interp1d功能。我试图让documentation page上的示例正常工作,但每当我运行它时,都会出现以下错误: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

所以,它适用于线性插值,但不适用于立方。我可能犯了一些愚蠢的错误,但我不知道出了什么问题。下面是我使用的示例代码: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()

编程小号
上一篇 2025-01-16 11:17
下一篇 2025-10-01 10:51

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/119630.html