While dealing arrays, there is need to round off float numbers to some decimal places or even to an integer.
So this recipe is a short example on how to round away from zero a float array. Let's get started.
import numpy as np
Let's pause and look at these imports. Numpy is generally used for working with arrays and performing mathematical operations in domain of linear algebra, fourier transform and matrices.
def round_array(x,y): return np.round(x,y)
This function takes x and y as input and return x rounded to y decimal places.
test = np.array([32.11, 51.5, 0.112])
We have simply setup one random array
Firslty, we call our round_array function sending in our test dataset and 0 (places to which it will be rounded.
Once we run the above code snippet, we will see:
[32. 52. 0.]