93

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?

5
  • 5
    np.round(data,2)? Commented Oct 28, 2017 at 21:00
  • 1
    Your example works just fine. What do you mean by "doesn't work"? Commented Oct 28, 2017 at 21:03
  • 3
    np.round is equivalent to np.around and both work for entire arrays. Commented Oct 28, 2017 at 21:03
  • What exactly do you want the rounded output to be? Commented Oct 28, 2017 at 21:03
  • I think he ment to say that how to reduce the numbers after decimal 1.60130719e-01 to 1.60 Commented May 30, 2019 at 9:37

4 Answers 4

142

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])
Sign up to request clarification or add additional context in comments.

2 Comments

array was not np array, after importing like np.array its working
Available also as method round(): a.round(decimals=2)
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.

Comments

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

1 Comment

This was more or less what I wanted. Using np.around instead of round will make it work for multidimensional arrays as well.
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.