如何使用scipy.interpolate.interp1d使用相同的X数组插入多个Y数组?

时间:2015-03-28 17:02:58

标签: python numpy scipy interpolation

作为一个例子,我有一个二维数据数组,其中一个维度上有误差条,例如:

In [1]: numpy as np
In [2]: x = np.linspace(0,10,5)
In [3]: y = np.sin(x)
In [4]: y_er = (np.random.random(len(x))-0.5)*0.1
In [5]: data = np.vstack([x,y,y_er]).T
In [6]: data
array([[  0.00000000e+00,   0.00000000e+00,  -6.50361821e-03],
       [  2.50000000e+00,   5.98472144e-01,  -3.69252108e-03],
       [  5.00000000e+00,  -9.58924275e-01,  -2.99042576e-02],
       [  7.50000000e+00,   9.37999977e-01,  -7.66584515e-03],
       [  1.00000000e+01,  -5.44021111e-01,  -4.24650123e-02]])

如果我想使用scipy.interpolate.interp1d,我该如何格式化它只需要调用一次?我想避免这种重复的方法:

In [7]: import scipy.interpolate as interpolate
In [8]: new_x = np.linspace(0,10,20)
In [9]: interp_y = interpolate.interp1d(data[:,0], data[:,1], kind='cubic')
In [10]: interp_y_er = interpolate.interp1d(data[:,0], data[:,2], kind='cubic')
In [11]: data_int = np.vstack([new_x, interp_y(new_x), interp_y_er(new_x)]).T
In [12]: data_int
Out[12]: 
array([[  0.00000000e+00,   1.33226763e-15,  -6.50361821e-03],
       [  5.26315789e-01,   8.34210211e-01,   4.03036906e-03],
       [  1.05263158e+00,   1.18950397e+00,   7.81676344e-03],
       [  1.57894737e+00,   1.17628260e+00,   6.43203582e-03],
       [  2.10526316e+00,   9.04947417e-01,   1.45265705e-03],
       [  2.63157895e+00,   4.85798968e-01,  -5.54638391e-03],
       [  3.15789474e+00,   1.69424684e-02,  -1.31694104e-02],
       [  3.68421053e+00,  -4.27201979e-01,  -2.03689966e-02],
       [  4.21052632e+00,  -7.74935541e-01,  -2.61377287e-02],
       [  4.73684211e+00,  -9.54559384e-01,  -2.94681929e-02],
       [  5.26315789e+00,  -8.97599881e-01,  -2.94003966e-02],
       [  5.78947368e+00,  -6.09763178e-01,  -2.60650399e-02],
       [  6.31578947e+00,  -1.70935195e-01,  -2.06835155e-02],
       [  6.84210526e+00,   3.35772943e-01,  -1.45246375e-02],
       [  7.36842105e+00,   8.27250110e-01,  -8.85721975e-03],
       [  7.89473684e+00,   1.21766391e+00,  -4.99008827e-03],
       [  8.42105263e+00,   1.39749683e+00,  -4.58031991e-03],
       [  8.94736842e+00,   1.24503605e+00,  -9.46430377e-03],
       [  9.47368421e+00,   6.38467937e-01,  -2.14799109e-02],
       [  1.00000000e+01,  -5.44021111e-01,  -4.24650123e-02]])

我相信会是这样的:

In [13]: interp_data = interpolate.interp1d(data[:,0], data[:,1:], axis=?, kind='cubic')

1 个答案:

答案 0 :(得分:3)

所以看着我的猜测,我曾尝试axis = 1。我仔细检查了唯一有意义的其他选项,axis = 0,并且它有效。所以对于下一个有同样问题的假人来说,这就是我想要的:

In [14]: interp_data = interpolate.interp1d(data[:,0], data[:,1:], axis=0, kind='cubic')
In [15]: data_int = np.zeros((len(new_x),len(data[0])))
In [16]: data_int[:,0] = new_x
In [17]: data_int[:,1:] = interp_data(new_x)
In [18]: data_int
Out [18]:
array([[  0.00000000e+00,   1.33226763e-15,  -6.50361821e-03],
       [  5.26315789e-01,   8.34210211e-01,   4.03036906e-03],
       [  1.05263158e+00,   1.18950397e+00,   7.81676344e-03],
       [  1.57894737e+00,   1.17628260e+00,   6.43203582e-03],
       [  2.10526316e+00,   9.04947417e-01,   1.45265705e-03],
       [  2.63157895e+00,   4.85798968e-01,  -5.54638391e-03],
       [  3.15789474e+00,   1.69424684e-02,  -1.31694104e-02],
       [  3.68421053e+00,  -4.27201979e-01,  -2.03689966e-02],
       [  4.21052632e+00,  -7.74935541e-01,  -2.61377287e-02],
       [  4.73684211e+00,  -9.54559384e-01,  -2.94681929e-02],
       [  5.26315789e+00,  -8.97599881e-01,  -2.94003966e-02],
       [  5.78947368e+00,  -6.09763178e-01,  -2.60650399e-02],
       [  6.31578947e+00,  -1.70935195e-01,  -2.06835155e-02],
       [  6.84210526e+00,   3.35772943e-01,  -1.45246375e-02],
       [  7.36842105e+00,   8.27250110e-01,  -8.85721975e-03],
       [  7.89473684e+00,   1.21766391e+00,  -4.99008827e-03],
       [  8.42105263e+00,   1.39749683e+00,  -4.58031991e-03],
       [  8.94736842e+00,   1.24503605e+00,  -9.46430377e-03],
       [  9.47368421e+00,   6.38467937e-01,  -2.14799109e-02],
       [  1.00000000e+01,  -5.44021111e-01,  -4.24650123e-02]])

我没有弄清楚使用np.vstacknp.hstacknew_x和插值数据合并在一行中的语法,但是此post让我停止尝试,因为它预分配数组似乎更快(例如使用np.zeros),然后用新值填充它。