Definition of NumPy Round
A Random Round is a function that works mathematically and performs a rounding off operation when we provide an input array of different float or decimal elements that we wanted to round the digits off to our desired number of positions and numpy round function by default do not need a decimal position argument by default it rounds off and we option if we wanted to declare the number of decimal positions we wanted to round.
Syntax:
The basic syntax of the numpy round function is,
numpy.round(array, decimals = 0, out = None)
- numpy.round represents the mathematical rounding off function in numpy.
- array represents the input array in which we wanted to perform the round off function.
- Decimals is an optional argument if wanted to declare the number of positions we wanted to round off.
- Out is the output array as a result of rounding off.
Examples of NumPy Round
Following are the examples as given below:
Example #1
Let us discuss a basic example for understanding how the numpy round function works.
Code:
import numpy as np
array1 = [0.25, 1.3, 3.5, 4.4, 5.5, 9.3]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1)
print ("Rounded values : ", round_array1)
array2 = [.23, 1.04, 0.571]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2)
print ("Rounded values : ", round_array2)
Output:
Here in the above example, we have declared two arrays, array1 & array2 as input for the numpy round function. We have printed the input arrays without rounding off and output arrays after rounding off. We can see the numpy round function on default rounds to single decimals when we give multiple digits after a decimal point. When we have 0.5 and above after the decimal point the numpy round functions rounds it to the next number in the series.
Example #2
In this example well discuss how we can use the decimal argument which allows us to round off to our desired values.
Code:
import numpy as np
array1 = [0.252, 1.354, 3.155, 4.345, 5.551, 9.23]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1, decimals = 2)
print ("Rounded values : ", round_array1)
array2 = [.2356, 1.554, 0.8571]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2, decimals = 3)
print ("Rounded values : ", round_array2)
Output:
We have declared two input arrays, array1 & array2 with digits more than 2 decimal points and for the first array we have declared the decimal round off point to be 2 and for the second array we have declared the decimal round off point to be 3 so that digits should not be generated in the output outside the decimal limits we have declared and the corresponding outputs has been printed in which we can clearly see the values getting rounded off before 2nd and 3rd digits in the 1st and 2nd outputs.
Example #3
Let’s try with different decimal position argument and check the output for better understanding.
Code:
import numpy as np
array1 = [0.25122, 1.31454, 31.6255, 41.4345, 25.5551, 39.8723]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1, decimals = 1)
print ("Rounded values : ", round_array1)
array2 = [112.23156, 210.53654, 90.85571]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2, decimals = 2)
print ("Rounded values : ", round_array2)
Output:
Example #4
Let us see in this example how the numpy round function works on integer values without decimal points and also how the decimal point argument can be used for whole numbers.
Code:
import numpy as np
array1 = [20, 10, 40, 100, 150]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1, decimals = 1)
print ("Rounded values : ", round_array1)
array2 = [120, 110, 40, 95, 150]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2, decimals = -2)
print ("Rounded values : ", round_array2)
array3 = [110, 140, 96, 75, 130]
print ("Input array2 : ", array3)
round_array3 = np.round_(array3, decimals = -3)
print ("Rounded values : ", round_array3)
Output:
In this example, we have three input array with integer values and in the first output statement, we have declared the decimal value to be 1 since the input array contains integer elements without any decimal values the numpy round function doesn’t work. So the output array is the same as the input since there is no scope to round the whole integers. In the second output we have declared the input array similar to the 1st array with all integers values without any decimal numbers and in the numpy round function, we have declared the decimal point value is negative (-2).
This negative two denotes the rounding off of two digits in reverse order so the resultant output printed is the nearest value with negative two digits rounding off. When the input has only two digits the negative rounding off gives 0 as the output.
Similarly in the 3rd output statement, we have declared the decimal point value to be rounded off as negative 3 (-3) so the resulting output will be negative 3 digits rounding off. Since the values in the array have all 3 digit elements the output array becomes all 0.
Example #5
Let’s try giving different inputs and perform the negative decimal value rounding off techniques to have a clear idea of the numpy round function. Depending upon the number of digits before and after the decimal points, we need to declare the positive and negative decimal value to be rounded off to get our expected result.
Code:
import numpy as np
array1 = [0.25122, 1.31454, 31.6255, 41.4345, 25.5551, 39.8723]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1, decimals = -1)
print ("Rounded values : ", round_array1)
array2 = [112.23156, 210.53654, 90.85571]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2, decimals = -2)
print ("Rounded values : ", round_array2)
Output:
Conclusion
In this article, we have discussed the Numpy round function in detail using various examples to get a clear understanding of numpy round function and its uses. We have also discussed how to round values using different whole integers, float value arrays. We also discussed the techniques used for declaring the decimal points that we wanted to round off this would be really helpful when working with arrays with decimal values.
Recommended Articles
This is a guide to NumPy Round. Here we also discuss the definition and syntax of numpy round along with different examples and its code implementation. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses