其他分享
首页 > 其他分享> > 我如何使用scipy.interpolate.interp1d使用相同的X数组插值多个Y数组?

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

作者:互联网

例如,我有一个二维数据数组,其中一个维度上带有误差条,如下所示:

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')

解决方法:

因此,根据我的猜测,我尝试了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.vstack或np.hstack将new_x和内插数据合并在一行中的语法,但是这个post让我停止尝试,因为似乎更快地预分配了数组(例如,使用np.zeros)然后用新值填充它.

标签:scipy,python,numpy,interpolation
来源: https://codeday.me/bug/20191120/2044846.html