python - How can I use scipy.interpolate.interp1d to interpolate multi Y arrays using the same X array? -


as example, have array of 2-d data error bars on 1 of dimensions, such this:

in [1]: numpy 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]]) 

if want use scipy.interpolate.interp1d, how format have call once? want avoid repeated method:

in [7]: import scipy.interpolate 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]]) 

i believe this:

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

so looking @ guess, had tried axis = 1. double checked other option made sense, axis = 0, , worked. next dummy has same problem, wanted:

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

i did not figure out syntax using np.vstack or np.hstack combine new_x , interpolated data in 1 line post made me stop trying seems faster pre-allocate array (e.g. using np.zeros) fill new values.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -