作为一个例子,我有一组具有2-D数据的数组,其中在一个维度上有误差棒,如下所示:
如果我想使用scipy.interpolate.interp1d,如何使其格式化只需调用一次?我想避免这种重复的方法:
我相信应该是这样的:
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')