[Numpy-discussion] how to round to int (I mean 5.2 to 5 not 5.0)
Sebastian Haase
haase at msg.ucsf.edu
Wed Mar 7 00:26:34 EST 2007
On 3/6/07, Alan G Isaac <aisaac at american.edu> wrote:
> 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
>
Would it be a useful / possible enhancement
to add an additional
dtype argument to round !?
So that if out is not given, but dtype is given (e.g. as N.int)
the "copy-array" is created with that dtype ?
Just a thought -- otherwise I might add it to my personal collection
of "useful" functions.
Thanks,
Sebastian Haase
More information about the NumPy-Discussion
mailing list