【问题标题】:Index error with scipy.interpolate.interp1dscipy.interpolate.interp1d 的索引错误
【发布时间】:2013-09-10 02:27:36
【问题描述】:

我正在尝试使三次样条与 scipy.interpolate.interp1d 函数一起使用。我试图让documentation page 上的示例工作,但每当我运行它时,我都会收到此错误:

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 中 结果 = 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 行,在 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() 

【问题讨论】:

  • 您的示例在我的计算机上运行正常,请尝试安装软件包的稳定版本。

标签: python numpy scipy


【解决方案1】:

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

【讨论】:

  • 您的脚本在 scipy 0.12.0 和 numpy 1.7.0 上运行良好
  • 是的,现在我检查了计算机上两个 python 发行版的 numpy 和 scipy 版本,我发现它适用于 scipy 0.12.0 和 numpy 1.7.1。它不适用于 scipy 0.12.0.dev-ddd617d 和 1.8.0.dev-9597b1f。不知道为什么我有疯狂的开发版本,但似乎它们以某种方式进入了计算机......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 2015-07-19
  • 2011-05-13
  • 2018-04-18
  • 2020-03-02
相关资源
最近更新 更多