Scipy.interpolate得到x值,产生某些y值2个数字列表

时间:2014-07-15 02:56:19

标签: python scipy interpolation

我有2个数据列表,一个是

x = [13.7,12.59,11.22,10.00,8.91,7.94,7.08,6.31,5.62,5.01,4.47,3.98,3.55,3.16,2.82,2.51,2.24,2.00,
    1.78,1.59,1.41,1.26,1.12,1.00,0.89,0.79,0.71,0.63,0.56,0.50,0.40,0.32,0.25,0.20,0.16,0.13,
   0.1,0.08,0.06,0.05,0.04,0.03,0.025,0.02,0.016,0.013,0.01,0.008,0.006,0.005,0.004]

一个是

y = [0.19 0.34 0.5 0.5 0.53 0.92 0.92 0.92 0.92 0.92 0.93 0.95 0.96 0.96 0.99 0.99 0.99 0.99 0.99 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0]

我需要在y=0.5,y=0.6,y=0.7

时获取x值

我有52对这种凌乱的数据,我需要得到给定y的x值,但y=0.5等不在y列表中。

我尝试为每对数据生成一些函数,然后尝试查找x值,但我不知道如何。

你能帮帮我吗?

1 个答案:

答案 0 :(得分:3)

interp1d函数是一种很好的方法,但您必须进行一些更改。假设我们在帖子中定义了x和y(注意我在列表y中添加了逗号和值1.0,以使它们具有相同的长度)。

x = [13.7, 12.59, 11.22, 10.0, 8.91, 7.94, 7.08, 6.31, 5.62, 5.01, 4.47, 3.98, 3.55, 3.16, 2.82, 2.51, 2.24, 2.0, 1.78, 1.59, 1.41, 1.26, 1.12, 1.0, 0.89, 0.79, 0.71, 0.63, 0.56, 0.5, 0.4, 0.32, 0.25, 0.2, 0.16, 0.13, 0.1, 0.08, 0.06, 0.05, 0.04, 0.03, 0.025, 0.02, 0.016, 0.013, 0.01, 0.008, 0.006, 0.005, 0.004]

y = [0.19, 0.34, 0.5, 0.5, 0.53, 0.92, 0.92, 0.92, 0.92, 0.92, 0.93, 0.95, 0.96, 0.96, 0.99, 0.99, 0.99, 0.99, 0.99, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

首先要注意的是scipy.interpolate.interp1d中的第一个参数必须单调递增。这意味着你可以这样做,

from scipy.interpolate import interp1d
f = interp1d( y, x )

x.reverse()
y.reverse()
f = interp1d( x, y )

在第一种情况下,我们得到了,

>>> f(0.5), f(0.6), f(0.7)
(array(11.22), array(8.735897435897437), array(8.487179487179487))

在第二个我们得到,

>>> f(0.5), f(0.6), f(0.7)
(array(1.0), array(1.0), array(1.0))