Using ndarray.round() method in NumPy (4 examples)

Updated: February 27, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a cornerstone library for numerical computations in Python. It offers extensive support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. Among its numerous array manipulation utilities, the ndarray.round() method is a straightforward yet potent tool for rounding off elements in an array. This tutorial explores the ndarray.round() method through four progressively advanced examples.

Syntax & Parameters

The ndarray.round() method in NumPy rounds an array to the specified number of decimals. Its basic syntax is:

array.round(decimals=0, out=None)

Parameters:

  • decimals: The number of decimal places to round to. It can be negative.
  • out: Alternative output array in which to place the result. It must have the same dimension as the expected output.

Example 1: Basic Rounding

Let’s start with a simple example to round elements of an array to the nearest integer.

import numpy as np

# Create a numpy array
arr = np.array([1.25, 2.5, 3.75, 4.8])

# Round elements of the array
rounded_arr = arr.round()
print(rounded_arr)

Output:

[1. 2. 4. 5.]

This result demonstrates how ndarray.round() method effectively rounds each element to the nearest integer.

Example 2: Specifying Decimal Places

In this example, we’ll round each element to two decimal places.

import numpy as np

# Create another array
arr = np.array([1.234, 2.456, 3.678, 4.891])

# Round elements to two decimal places
rounded_arr = arr.round(decimals=2)
print(rounded_arr)

Output:

[1.23 2.46 3.68 4.89]

By specifying decimals=2, we control the precision of the rounding to our desired level.

Example 3: Rounding with Negative Decimals

The ndarray.round() method allows for negative decimal values, which rounds the numbers to the left of the decimal point. This can be particularly useful when working with large numbers and you want to approximate them to the nearest ten, hundred, or thousand. Let’s see an example where we round to the nearest ten.

import numpy as np

# Large numbers array
arr = np.array([123, 456, 789, 1011])

# Round to the nearest ten
rounded_arr = arr.round(decimals=-1)
print(rounded_arr)

Output:

[120 460 790 1010]

This method helps in simplifying the numbers for easier interpretation or for meeting specific numeric formats.

Example 4: Rounding with Output Array

Finally, let’s explore how to use the out parameter. This feature can be useful when you want to save memory by avoiding the creation of a new array for the rounded results. Instead, the results are stored in an existing array passed through the out parameter.

import numpy as np

# Original array
arr = np.array([1.2345, 2.3456, 3.4567, 4.5678])

# Pre-allocated array for output
out_arr = np.empty(arr.shape)

# Round elements and store in out_arr
arr.round(decimals=2, out=out_arr)
print(out_arr)

Output:

[1.23 2.35 3.46 4.57]

This example illustrates the utility of the out parameter, especially when dealing with large datasets.

Conclusion

The ndarray.round() method is a versatile function for approximating numeric values within a NumPy array. Through the examples provided, we’ve seen how it can be adapted to various rounding necessities, from basic rounding operations to more complex scenarios where memory optimization is required. With this understanding, you can now effortlessly incorporate rounding into your numerical computations.