Python:NumPy .rint()
Published Nov 28, 2024
Contribute to Docs
In NumPy, the .rint() method rounds each element of an array to the nearest integer. It returns a new array with rounded values, preserving the input’s shape.
Syntax
numpy.rint(x, /, out=None, *, where=True)
x: The input array containing elements that will be rounded to the nearest integer.out: A location where the result is stored. If provided, the result will be stored in this array. It must have the same shape asx.where: This condition determines where to apply the operation. IfTrue, the operation is performed on all elements. IfFalse, it is skipped for the corresponding element.
Example
In the example below, the .rint() method rounds each element of the array to the nearest integer:
import numpy as npx = np.array([1.2, 2.7, 3.5, 4.1])rounded_x = np.rint(x)print(rounded_x)
The above code outputs the following:
[1. 3. 4. 4.]
Codebyte Example
Run the following codebyte example to understand how the .rint() method works:
Here, .rint() rounds each element in the array x to the nearest integer, and the result is stored in the array rounded_x.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours