Applying math.ceil to an array in python -


what proper way apply math.ceil entire array? see following python code:

    index = np.zeros(len(any_array))     index2 = [random.random() x in xrange(len(any_array))     ##indexfinal=math.ceil(index2)  <-? 

and want return ceiling value of every element within array. documentation states math.ceil returns ceiling input x, best method of applying ceiling function every element contained within array?

use numpy.ceil() function instead. numpy package offers vectorized versions of of standard math functions.

in [29]: import numpy np in [30]: = np.arange(2, 3, 0.1) in [31]: out[31]: array([ 2. ,  2.1,  2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9])  in [32]: np.ceil(a) out[32]: array([ 2.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.]) 

this technique should work on arbitrary ndarray objects:

in [53]: a2 = np.indices((3,3)) * 0.9  in [54]: a2 out[54]: array([[[ 0. ,  0. ,  0. ],         [ 0.9,  0.9,  0.9],         [ 1.8,  1.8,  1.8]],         [[ 0. ,  0.9,  1.8],         [ 0. ,  0.9,  1.8],         [ 0. ,  0.9,  1.8]]])  in [55]: np.ceil(a2) out[55]: array([[[ 0.,  0.,  0.],         [ 1.,  1.,  1.],         [ 2.,  2.,  2.]],         [[ 0.,  1.,  2.],         [ 0.,  1.,  2.],         [ 0.,  1.,  2.]]]) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -