How to round a numpy array?
Asked Answered
D

4

89

I have a numpy array, something like below:

data = np.array([  1.60130719e-01,   9.93827160e-01,   3.63108206e-04])

and I want to round each element to two decimal places.

How can I do so?

Dynamiter answered 28/10, 2017 at 20:59 Comment(5)
np.round(data,2)?Brodie
Your example works just fine. What do you mean by "doesn't work"?Expletive
np.round is equivalent to np.around and both work for entire arrays.Isherwood
What exactly do you want the rounded output to be?Hooey
I think he ment to say that how to reduce the numbers after decimal 1.60130719e-01 to 1.60Tartary
B
140

Numpy provides two identical methods to do this. Either use

np.round(data, 2)

or

np.around(data, 2)

as they are equivalent.

See the documentation for more information.


Examples:

>>> import numpy as np
>>> a = np.array([0.015, 0.235, 0.112])
>>> np.round(a, 2)
array([0.02, 0.24, 0.11])
>>> np.around(a, 2)
array([0.02, 0.24, 0.11])
>>> np.round(a, 1)
array([0. , 0.2, 0.1])
Bink answered 28/10, 2017 at 21:2 Comment(2)
array was not np array, after importing like np.array its workingDynamiter
Available also as method round(): a.round(decimals=2)Arms
D
10

It is worth noting that the accepted answer will round small floats down to zero as demonstrated below:

>>> import numpy as np 
>>> arr = np.asarray([2.92290007e+00, -1.57376965e-03, 4.82011728e-08, 1.92896977e-12])
>>> print(arr)
[ 2.92290007e+00 -1.57376965e-03  4.82011728e-08  1.92896977e-12]
>>> np.round(arr, 2)
array([ 2.92, -0.  ,  0.  ,  0.  ]) 

You can use set_printoptions and a custom formatter to fix this and get a more numpy-esque printout with fewer decimal places:

>>> np.set_printoptions(formatter={'float': "{0:0.2e}".format})
>>> print(arr)
[2.92e+00 -1.57e-03 4.82e-08 1.93e-12]  

This way, you get the full versatility of format and maintain the precision of numpy's datatypes.

Also note that this only affects printing, not the actual precision of the stored values used for computation.

Destroyer answered 12/5, 2020 at 21:44 Comment(0)
H
8

If you want the output to be

array([1.6e-01, 9.9e-01, 3.6e-04])

the problem is not really a missing feature of NumPy but rather that this sort of rounding is not a standard thing to do. You can make your own rounding function which achieves this like so:

def my_round(value, N):
    exponent = np.ceil(np.log10(value))
    return 10**exponent*np.round(value*10**(-exponent), N)

For a general solution handling 0 and negative values as well, you can do something like this:

def my_round(value, N):
    value = np.asarray(value).copy()
    zero_mask = (value == 0)
    value[zero_mask] = 1.0
    sign_mask = (value < 0)
    value[sign_mask] *= -1
    exponent = np.ceil(np.log10(value))
    result = 10**exponent*np.round(value*10**(-exponent), N)
    result[sign_mask] *= -1
    result[zero_mask] = 0.0
    return result
Hooey answered 28/10, 2017 at 21:19 Comment(1)
This was more or less what I wanted. Using np.around instead of round will make it work for multidimensional arrays as well.Gilbertina
D
0

if you want to update your array named "data" you can do one of the following. Here the digit 2 inside the functions states that the elements are to be rounded to two decimals.

import numpy as np
np.round(data,2,data)

or

import numpy as np
data.round(2,data)

or

import numpy as np
np.around(data,2,data)
Devotion answered 4/5, 2024 at 21:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.