[Numpy-discussion] how to round to int (I mean 5.2 to 5 not 5.0)

Alan G Isaac aisaac@american....
Tue Mar 6 23:02:59 CST 2007


On Tue, 6 Mar 2007, Sebastian Haase apparently wrote: 
> why does 
> numpy.round(a) 
> return a float ? 

Because it works with a copy of a.

>>> help(N.round)
Help on function round_ in module numpy.core.fromnumeric:
round_(a, decimals=0, out=None)
    Returns reference to result. Copies a and rounds to 'decimals' places.

    Keyword arguments:
        decimals -- number of decimal places to round to (default 0).
        out -- existing array to use for output (default copy of a).

    Returns:
        Reference to out, where None specifies a copy of the original array a.
>>> a = N.random.random((10,))*100
>>> a
array([ 45.01971148,   8.32961759,  39.75272544,  79.76986159,
        23.66331127,  24.25584246,  38.17354106,  16.57977389,
        50.63676986,  83.15113716])
>>> b = N.empty((10,),dtype='int')
>>> N.round(a,out=b)
array([45,  8, 40, 80, 24, 24, 38, 17, 51, 83])
>>> b
array([45,  8, 40, 80, 24, 24, 38, 17, 51, 83])


Cheers,
Alan Isaac






More information about the Numpy-discussion mailing list