NumPy rint() Function:
The rint() function of the NumPy module is used to round array elements to the nearest integer.
Syntax:
numpy.rint(x, out=None)
Parameters
x: This is required. It is an array (array-like) given as input.
out: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.
Return Value:
If out=None, returns an array containing the rounded values. A reference to out is returned if an output array is given.
NumPy rint() Function in Python
Example1
Here is an example for 1D Arrays
Approach:
- Import numpy module using the import keyword.
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the above-given array as an argument to the rint() function of the numpy module to round the given array elements to the nearest integer.
- Store it in another variable.
- Print the given array elements after rounding to the nearest integer.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([20.3, 5.5, 8.8, 30.076, 12.123]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the above given array as an argument to the rint() function of the # numpy module to round the given array elements to the nearest integer. # Store it in another variable. rslt = np.rint(gvn_arry) # Print the given array elements after rounding to the nearest integer. print("The given array elements after rounding to the nearest integer:") print(rslt)
Output:
The above given array is: [20.3 5.5 8.8 30.076 12.123] The given array elements after rounding to the nearest integer: [20. 6. 9. 30. 12.]
Example2
Here is an example for 2-Dimensional Arrays
Approach:
- Import numpy module using the import keyword.
- Pass some random list(2-Dimensional) as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the above-given array as an argument to the rint() function of the numpy module to round the given array elements to the nearest integer.
- Store it in another variable.
- Print the given array elements after rounding to the nearest integer.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list(2D) as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([[-2.3, 6.6],[4.5, -8.9]]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the above given array as an argument to the rint() function of the # numpy module to round the given array elements to the nearest integer. # Store it in another variable. rslt = np.rint(gvn_arry) # Print the given array elements after rounding to the nearest integer. print("The given array elements after rounding to the nearest integer:") print(rslt)
Output:
The above given array is: [[-2.3 6.6] [ 4.5 -8.9]] The given array elements after rounding to the nearest integer: [[-2. 7.] [ 4. -9.]]