Instagram
youtube
Facebook
Twitter

Round Elements of NumPy Array to Nearest Integer

A Round Elements of NumPy Array to Nearest Integer?

Code Explanation:
Library Import: numpy is imported as np to perform array operations.
Array Creation: A NumPy array arr is created with integer elements.
Condition Check: arr > 5 checks which elements are greater than 5 and returns a boolean array.
Boolean Mask: The result is stored in mask, where True means the value is greater than 5.
Display Output: print(mask) prints the boolean array: [False False True False True].

 

Program:

import numpy as np

arr = np.array([1.2, 3.7, 2.5])

rounded = np.rint(arr)

print(rounded)