Rounding Decimals In NumPy Ufunc
In NumPy, there are several functions for rounding decimals, each with its own objective and execution.
The purpose of this article is to explore the different ways in which we can round decimals when utilizing NumPy.
Numpy ufuncs include several functions that can be used to round decimals in arrays.
There are mainly five most used functions to rounding decimals:
|Truncation||Truncation is the process of removing the decimal part of a number and returning the integer part.
Positive numbers are truncated towards zero while negative numbers are truncated towards negative infinity.
|Fix||Fix is a function that rounds a number towards zero, returning the nearest integer towards zero.
If the number is positive, it rounds down to the nearest integer. If the number is negative, it rounds up to the nearest integer.
|Rounding||Rounding is the process of approximating a number to a specified number of decimal places.
There are several methods of rounding, including rounding up, rounding down, rounding to the nearest even number, and rounding to the nearest odd number.
|Floor||Floor is a function that rounds a number down to the nearest integer or decimal.
The result is always less than or equal to the original number.
The floor function is useful for converting continuous variables into categorical variables.
|Ceil||Ceil is a function that rounds a number up to the nearest integer or decimal.
The result is always greater than or equal to the original number.
The ceil function is useful for converting continuous variables into categorical variables, similar to the floor function.
Truncation is a quick and simple way to remove the decimal part of a number and obtain the integer part.
It can be useful when working with financial data, where it is often necessary to work with whole numbers only.
You can utilize the functions trunc() and fix() to perform the operation.
In the following mrx_arr array, truncate the items as follows:
Truncate the element first in the below array then apply the absolute() function:
Fix is useful when you need to round a number towards zero.
It can be helpful when dealing with data that needs to be rounded down or up depending on the sign of the number.
Here is the identical example as above but utilizing the fix() function:
Implement the absolute() function after the fix() function:
According to rounding decimals, the around() function increases the preceding digit by 1 if the value is higher than 5, otherwise, it does nothing.
The number 7.2545 can be rounded off to 2 decimal points, and it is going to be 7.25
Calculate 7.0 by rounding off 7.2545:
Rounding can help you obtain a more simplified representation of a number.
It can be useful when working with measurements or data that needs to be reported in a specific format or precision.
In the following example, round off -26.5644 to 2 decimal positions:
In the floor() function, decimal numbers are rounded to the next smaller integer.
For example, the floor of -6.2333 is -7.
In the following array, floor number 5.923 is as follows:
Floor is helpful when you need to round a number down to the nearest integer or decimal.
It can be useful when dealing with categorical data, as you can use floor to convert continuous variables into categories.
Floor the following numbers in the mrx_arr array:
A decimal value is rounded to the closest higher integer by calling the ceil() function.
For example, the ceiling of 6.2111 is 7.
In the following array, ceil number 6.2111 is as follows:
Ceil can be useful when you need to round a number up to the nearest integer or decimal.
It is also helpful for converting continuous variables into categories, similar to floor.
Ceil the values of the following mrx_arr array then execute the absolute() function:
Above example creates a list of three negative numbers, [-3.94, -25.45, -0.67], and applies two NumPy functions to it: npy.ceil() and npy.absolute().
npy.ceil() takes the ceiling of each number in the list, which means it rounds up to the nearest integer. So the resulting list is [-3, -25, -0].
npy.absolute() takes the absolute value of each number in the resulting list.
The absolute value is the distance of a number from zero, so it makes all the values positive. So the final output of the code is shown in below Image.