5. Write a Python Numpy program to round all the numbers in the initial array to its nearest whole number.
The output is:
Initial array = [0.1, 2.4, -7.8, 3.7, 4.3, 5.6]
Expected Answer:
Initial array: [ 0.1 2.4 -7.8 3.7 4.3 5.6]
Round to nearest whole number: [ 0. 2. -8. 4. 4. 6.]
Sample Answer:
import numpy as np a = np.array([0.1, 2.4, -7.8, 3.7, 4.3, 5.6]) print("Initial array:", a) b = np.rint(a) print("\nRound to nearest whole number:", b)
More Exercises:
Python String ExercisesMore Numpy Exercises:
Numpy String ExercisesMore Pandas Exercises:
Pandas Series ExercisesMore Tutorials:
Python Installation - Linux (Ubuntu)