np.around: What is Numpy around() Function in Python

The np.around() method is defined under Numpy, which can be imported as import numpy as np. We can create multidimensional arrays and derive other mathematical statistics with the help of numpy, a library in Python.

np.around

The np.around() is a mathematical function that helps the user to evenly round array elements to the given number of decimals. The numpy around() function takes up to three parameters and returns an array with all the elements being rounded off to the specified value.

Syntax

numpy.around(array, decimal(int), out(output array))

Parameters

It takes three parameters out of which two parameters are optional. 

The first parameter is an array that takes the input array 

The second parameter is the decimal, which takes an integer value that defines out the places up to which we have to round off. 

The third and last parameter outputs the resulted array.

Return Value

The function returns an array with all the elements being rounded off to the specified value.

Example programs on numpy around() method

Write the code to show the working of around() function in Python.

See the following code.

import numpy as np

arr = [1.35, 23.6, 77.2, 54.33, 11.3]
print("Input array = ", arr)
print("\nOutput array: = ", np.around(arr))

Output

Input array =  [1.35, 23.6, 77.2, 54.33, 11.3]

Output array: =  [ 1. 24. 77. 54. 11.]

In this example, we can see that after taking five float values in an array, we are getting output as rounded off elements. 

For example, 1.35 is rounded off to 1, then 23.6 is rounded off to 24, and so on for the rest of the elements.

Write a program to pass integer values as elements of the array and then use around() function and print the output.

See the following code.

import numpy as np

arr = [64, 22, 11, 300, 463, 127]
print("Input array = ", arr)
print("\nOutput array: = ", np.around(arr, decimals = -2))

Output

Input array =  [64, 22, 11, 300, 463, 127]

Output array: =  [100   0   0 300 500 100]

In this example, we can see that when we passed the second parameter as -2, it is rounding off the array to its hundred places. For example, 64 is rounded off to 100, and 22 is rounded off to 0, and so on.

That’s it for np.around() method.

See also

Numpy put()

Numpy apply_over_axes()

Numpy apply_along_axis()

Numpy all()

Numpy any()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.