4

I have some calculation involving two matrices both represented in numpy arrays.

After the calculation, i obtain a vector of floats represented in another numpy array.

I want to round up/down the values in this resultant vector, e.g. if the calculation gives:

array([1.33333, 2.56, 9.99999, 16.0])

then it should be rounded to:

array([1, 3, 10, 16])

What is the fastest way to do this?

1 Answer 1

10

NumPy arrays have a round method:

In [73]: x = np.array([1.33333, 2.56, 9.99999, 16.0])

In [74]: x.round()
Out[76]: array([  1.,   3.,  10.,  16.])
Sign up to request clarification or add additional context in comments.

2 Comments

does this method depend on the type of floats in the array? e.g. float32 vs. float64?
If x is of dtype float32, then x.round() will also be of dtype float32. And similarly for float64. The round method is not implemented for some dtypes, for example, string dtypes.

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.