使用Scipy插入不连续函数

时间:2011-11-25 11:36:54

标签: python interpolation

我在使用Scipy插入一些数据点时遇到问题。我想这可能取决于我试图插值的函数在x大约4处是不连续的。

以下是我用于插值的代码:

  from scipy import *
  y_interpolated = interp1d(x,y,buonds_error=False,fill_value=0.,kind='cubic')
  new_x_array = arange(min(x),max(x),0.05)
  plot(new_x_array,x_interpolated(new_x_array),'r-')

我得到的错误是

  File "<stdin>", line 2, in <module>
  File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 357, in __call__
    out_of_bounds = self._check_bounds(x_new)
  File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 415, in _check_bounds
    raise ValueError("A value in x_new is above the interpolation "
ValueError: A value in x_new is above the interpolation range.

这些是我的数据点:

  1.56916432074 -27.9998263169
  1.76773750527 -27.6198430485
  1.98360238449 -27.2397962268
  2.25133982943 -26.8596491107
  2.49319293195 -26.5518194791
  2.77823462692 -26.1896935372
  3.07201297519 -25.9540514619
  3.46090507092 -25.7362456112
  3.65968688527 -25.6453922172
  3.84116464506 -25.53652509
  3.97070419447 -25.3374215879
  4.03087127145 -24.8493356465
  4.08217147954 -24.0540196233
  4.12470899596 -23.0960856364
  4.17612639206 -22.4634289328
  4.19318305992 -22.1380894034
  4.2708234589 -21.902951035
  4.3745696768 -21.9027079759
  4.52158254627 -21.9565591238
  4.65985875536 -21.8839570732
  4.80666329863 -21.6486676004
  4.91026629192 -21.4496126386
  5.05709528961 -21.2685401725
  5.29054655428 -21.2860476871
  5.54129211534 -21.3215908912
  5.73174988353 -21.6645019816
  6.06035782465 -21.772138994
  6.30243916407 -21.7715483093
  6.59656410998 -22.0238656166
  6.86481948673 -22.3665921479
  7.01182409559 -22.4385289076
  7.17609125906 -22.4200564296
  7.37494987052 -22.4376476472
  7.60844044988 -22.5093814451
  7.79869207061 -22.5812017094
  8.00616642549 -22.5445612485
  8.17903446593 -22.4899243886
  8.29141325457 -22.4715846981

1 个答案:

答案 0 :(得分:5)

您使用的是什么版本的scipy

您发布的脚本有一些语法错误(我假设由于错误的复制和粘贴)。 此脚本与scipy.__version__ == 0.9.0一起使用。 Result picture here

import sys
from scipy import *
from scipy.interpolate import *
from pylab import plot

x = []
y = []
for line in sys.stdin:
   a, b = line.split()
   x.append(float(a))
   y.append(float(b))

y_interpolated = interp1d(x,y,bounds_error=False,fill_value=0.,kind='cubic')
new_x_array = arange(min(x),max(x),0.05)
plot(new_x_array,y_interpolated(new_x_array),'r-')