scipy spline interpolation with interp1d IndexError
我正在尝试使用scipy,并且在使用\\\\ linear \\'以外的其他类型时(似乎尝试了\\'zero \\'和\\ '立方体\\')。
在Google中搜索时找不到相同的问题,所以我觉得这很愚蠢。
我在OSX 10.8的python 2.73上使用scipy 0.11
代码不起作用:
1 2 3 4 5 6 7 8 9 10 | from scipy import interpolate import numpy as np x = np.arange(0, 10) y = np.exp(-x/3.0) f = interpolate.interp1d(x, y, kind="zero") xnew = np.arange(0,9, 0.1) ynew = f(xnew) # use interpolation function returned by `interp1d` plt.plot(x, y, 'o', xnew, ynew, '-') plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | IndexError Traceback (most recent call last) <ipython-input-1-23bb96a1589b> in <module>() 4 f = interpolate.interp1d(x, y, kind="zero") 5 xnew = np.arange(0,9, 0.1) ----> 6 ynew = f(xnew) # use interpolation function returned by `interp1d` 7 plt.plot(x, y, 'o', xnew, ynew, '-') 8 plt.show() /usr/local/lib/python2.7/site-packages/scipy/interpolate/interpolate.pyc in __call__(self, x_new) 394 out_of_bounds = self._check_bounds(x_new) 395 --> 396 y_new = self._call(x_new) 397 398 # Rotate the values of y_new back so that they correspond to the /usr/local/lib/python2.7/site-packages/scipy/interpolate/interpolate.pyc in _call_spline(self, x_new) 370 def _call_spline(self, x_new): 371 x_new =np.asarray(x_new) --> 372 result = spleval(self._spline,x_new.ravel()) 373 return result.reshape(x_new.shape+result.shape[1:]) 374 /usr/local/lib/python2.7/site-packages/scipy/interpolate/interpolate.pyc in spleval((xj, cvals, k), xnew, deriv) 833 res[sl].imag = _fitpack._bspleval(xx,xj,cvals.imag[sl],k,deriv) 834 else: --> 835 res[sl] = _fitpack._bspleval(xx,xj,cvals[sl],k,deriv) 836 res.shape = oldshape + sh 837 return res IndexError: too many indices |
在这里启动调试控制台时,我可以将其范围缩小到cvals [sl],从而导致错误,
1 2 3 | sl = (slice(None, None, None), 0) # <-- I don't really get the slice part here... cvals = array([ 1. , 0.71653131, 0.51341712, 0.36787944, 0.26359714, 0.1888756 , 0.13533528, 0.09697197, 0.06948345]) |
有人可以重现此内容,或者我的机器上有问题吗?
似乎,在OSX上按照scipy的安装说明进行操作时,我安装了numpy的非发行版,但没有意识到。
使用pip重新安装numpy,scipy和matplotlib解决了此问题。
非常感谢unutbu指出了问题的根源。