大家好,又见面了,我是你们的朋友全栈君。
我试图得到一个三次样条函数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()
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/135815.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...