np.round() function in Python [6 Examples]

In this NumPy tutorial, I will explain what the np.round() function in Python is, discussing its syntax, parameters, return value, and some illustrative examples.

To round elements in a NumPy array to a specified number of decimal places, the np.round() function in Python is used. It efficiently handles large arrays and adheres to the “round half to even” rule for midpoint values.

np.round() function in Python

The np.round() function in Python, part of the NumPy library, is used for rounding elements in an array to a specified number of decimal places. It takes an array and an optional ‘decimals’ argument, which defaults to zero if not provided. The function returns a new array with rounded values, without altering the original array.

Python NumPy round() function syntax

The basic syntax of the np.round() function is as follows:

numpy.round(arr, decimals=0, out=None)

NumPy round() function parameters required

Here,

NameDescription
arrThis is the input array in Python that we want to round,
decimalsAn integer that determines the number of decimal places to which the values are rounded. The default value is 0.
outThis is an optional parameter. If provided, it should be a Python array of the same shape as arr. The rounded values will be placed into this array.
List of parameters required in the numpy.round() function in Python.

Python round() function return value

The np.round() function in Python returns an array of the same shape as the input array (arr), containing the rounded values. If the out parameter is used, the return is a reference to this array.

READ:  Attributeerror module 'tensorflow' has no attribute 'summary'

np round function use cases in Python

Let’s see some examples that will demonstrate the np.round() function in Python.

1. How to round in Python using the np.round() function

Here, we are rounding elements of a NumPy array representing various measurements to two decimal places in Python.

import numpy as np

rainfall_data = np.array([41.5678, 15.1234, 36.5647])
rounded_rainfall = np.round(rainfall_data, 2)
print(rounded_rainfall)

Output:

[41.57 15.12 36.56]

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

np.round() function in Python

2. NumPy round significant 0

To adjust a set of floating-point numbers to the nearest integer through the np.round() function in Python.

import numpy as np

population_data = np.array([1.5678, 2.1234, 3.5647])
rounded_population = np.round(population_data)
print(rounded_population)

Output:

[2. 2. 4.]

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

np.round in Python

3. NumPy round with the out parameter

Let me demonstrate efficient data handling by rounding numbers and storing them directly in a pre-allocated array in Python using the out parameter in the NumPy round() function.

import numpy as np

stock_prices = np.array([123.5678, 234.1234, 345.5647])
rounded_prices = np.empty(stock_prices.shape)
np.round(stock_prices, 0, out=rounded_prices)
print(rounded_prices)

Output:

[124. 234. 346.]

After executing the code in Pycharm, one can see the output in the below screenshot.

numpy round down in Python

4. NumPy round to significant figures

Let’s create a custom Python function with the np.round() to round a specific number to a defined number of significant figures.

import numpy as np
def round_to_significant_figures(num, sig_figs):
    if num != 0:
        order_of_magnitude = np.floor(np.log10(np.abs(num)))
        scale_factor = 10**(order_of_magnitude - (sig_figs - 1))
        return np.round(num / scale_factor) * scale_factor
    else:
        return 0

bridge_length = 1234.5678
rounded_length = round_to_significant_figures(bridge_length, 3)
print(rounded_length)

Output:

1230.0

After the implementation of the code in the Pycharm editor, the screenshot is mentioned below

numpy significant figures in Python

5. NumPy round significant digits

Here, we will create a custom function to round a different number to a precise number of significant digits in Python.

import numpy as np
def round_to_significant_figures(num, sig_figs):
    if num != 0:
        order_of_magnitude = np.floor(np.log10(np.abs(num)))
        scale_factor = 10**(order_of_magnitude - (sig_figs - 1))
        return np.round(num / scale_factor) * scale_factor
    else:
        return 0

concentration = 0.012345
rounded_concentration = round_to_significant_figures(concentration, 4)
print(rounded_concentration)

Output:

0.01234

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

python numpy round up

6. np.round() in Python for large arrays

Let’s use np.round to compute the world population every year, rounded to the nearest million (6 zeroes). give the result (an array of 66 numbers) the name population_rounded. Your code should be very short.

import numpy as np

world_population = np.array([2558000000, 2595000000, 2637000000, 2682000000, 2730000000, 2782000000, 2835000000, 2891000000, 2948000000, 3001000000, 3043000000, 3084000000, 3140000000, 3210000000, 3281000000, 3350000000, 3421000000, 3490000000, 3562000000, 3637000000, 3713000000, 3790000000, 3867000000, 3942000000, 4017000000, 4089000000, 4160000000, 4232000000, 4304000000, 4379000000, 4451000000, 4534000000, 4615000000, 4696000000, 4775000000, 4856000000, 4941000000, 5027000000, 5115000000, 5201000000, 5289000000, 5372000000, 5456000000, 5538000000, 5619000000, 5699000000, 5779000000, 5858000000, 5935000000, 6012000000, 6089000000, 6165000000, 6242000000, 6319000000, 6396000000, 6473000000, 6551000000, 6630000000, 6709000000, 6788000000, 6866000000, 6944000000, 7022000000, 7101000000, 7179000000, 7256000000])

population_rounded = np.round(world_population / 1e6) * 1e6
print(population_rounded)

Output:

[2.558e+09 2.595e+09 2.637e+09 2.682e+09 2.730e+09 2.782e+09 2.835e+09
 2.891e+09 2.948e+09 3.001e+09 3.043e+09 3.084e+09 3.140e+09 3.210e+09
 3.281e+09 3.350e+09 3.421e+09 3.490e+09 3.562e+09 3.637e+09 3.713e+09
 3.790e+09 3.867e+09 3.942e+09 4.017e+09 4.089e+09 4.160e+09 4.232e+09
 4.304e+09 4.379e+09 4.451e+09 4.534e+09 4.615e+09 4.696e+09 4.775e+09
 4.856e+09 4.941e+09 5.027e+09 5.115e+09 5.201e+09 5.289e+09 5.372e+09
 5.456e+09 5.538e+09 5.619e+09 5.699e+09 5.779e+09 5.858e+09 5.935e+09
 6.012e+09 6.089e+09 6.165e+09 6.242e+09 6.319e+09 6.396e+09 6.473e+09
 6.551e+09 6.630e+09 6.709e+09 6.788e+09 6.866e+09 6.944e+09 7.022e+09
 7.101e+09 7.179e+09 7.256e+09]

After executing the code in Pycharm, one can see the output in the below screenshot.

python round function in NumPy

Conclusion

Here, I have illustrated the versatility of the np.round() function in Python and considered its applications with different parameters: rounding an array to a specific number of decimal places, adjusting values to the nearest integer, or directly storing the rounded results in a pre-allocated array.

READ:  Matplotlib unknown projection '3d'

You may also like to read: