In this article, we focus on one of the functions of the NumPy module in Python ie Numpy round_. NumPy package mainly focuses on routines for processing of array and Numpy round_() is utilized to return the round value of array elements.
Also read: Numpy ceil – Return the ceiling of the input, element-wise
What is NumPy round_?
Numpy Round is a mathematical function of the NumPy module used in a python programming language. It is used for manipulating Numpy arrays.
To be more specific NumPy round rounds numbers. eg: np.round_(3.18)
will return 3, as 3.18~3. When applied to a 1-dimensional array of numbers it will apply np.round_()
to each element of the array.
Syntax of numpy.round_()
Note – here we have imported NumPy as np thus using its alias during implementation.
np.round(arr, decimals=0,out='none')
Parameter | Description | Required/Optional |
arr | The input array. | Required |
decimals | The number of decimal places to which the input must be rounded | Optional |
out | alternative array to store the result | Optional |
Return Value
An array containing all the array elements which are rounded off having the same data type as input.
Also check: NumPy around – A Complete Guide
Implementing numpy.round_()
- If the number to be rounded is followed by a number greater than or equal to 5 ie 5,6,7,8,9 then round the number up. eg: 38 is rounded to 40 as 8>5 similarly 3.8 will be rounded as 4.
- If the number to be rounded is followed by a number smaller than 5 ie 0,1,2,3,4 then round the number down. eg: 32 is rounded to 30 as 2>5 and similarly 4.3 will be rounded as 4.
Examples of numpy.round_()
Importing numpy library
import numpy as np
Example 1. Rounding an array downward
Below is the code used to implement np.round_()
function to calculate the round-off values of each element of the input array.
x = np.round_([1.2,3.4,5.3])
print(x)
In the output, an array containing 3 round-off values is returned. As the numbers to be rounded is followed by a number smaller than 5, we round the number downwards.
Output :
[1. 3. 5.]
Example 2. Rounding an array upward
x = np.round_([1.6,3.5,5.8])
print(x)
6, 5, and 8 (digits after the decimal in the input array) are greater than 5 thus leading to upward rounding of the result and we will get an output as the next integer to the float value.
Output :
[2. 4. 6.]
Example 3. Rounding a negative array
The negative sign doesn’t have an adverse effect on the rules. We can follow the same as for the positive numbers and get the nearest rounded integer.
x = np.round_([-1.2,-3.4,-5.8])
print(x)
Output :
[-1. -3. -6.]
Example 4. Rounding an array to a specific decimal
x=np.round_([3.424,6.735,.503],decimals=2)
print(x)
Specifying the decimals (int the parameter decimals)allows us to manage the precision we want after rounding.
Output :
[3.42 6.74 0.5 ]
Conclusion
In this article, we learned the NumPy round_ function and how to utilize it to round off elements in an array to a specific decimal.
Reference
http://academic.brooklyn.cuny.edu/geology/leveson/core/linksa/roundoff.html