×

NumPy Rounding and Commonly used functions

Rounding functions:

The rounding functions are the functions used to round a number to a specified number of digits or decimals. The NumPy package has several functions to perform rounding operations.

  • around()
  • rint()
  • fix()
  • floor()
  • ceil()
  • trunc()

around():

The around() function of NumPy is used to evenly round to the given number of decimals.

Syntax:

around(a, decimals=0)

The a parameter is the input array. The decimals parameter is the number of decimal places to round(default is 0).

import numpy as np
arr = np.array([1.2332, 4.7863, 5.3526, 7.3345])
print(np.around(arr, decimals = 2))

#Output:
[1.23 4.79 5.35 7.33]

The NumPy round_() function also works similarly to around() function and used  for rounding values.

print(np.round_(arr, decimals = 1))

#Output:
[1.2 4.8 5.4 7.3]

rint():

The Numpy rint() function rounds the elements of a given array to the nearest integer.

Syntax:

rint(x)

The x parameter is the input array.

arr = np.array([1.2332, 4.7863, 5.3526, 7.3345])
print(np.rint(arr))

#Output:
[1. 5. 5. 7.]

fix():

The fix() function is used to round the array elements to the nearest integer towards zero. The fix() function rounds an array of floats element-wise and returns an array of datatype float.

Syntax:

fix(x)

The x parameter is the input array of floats to be rounded.

arr = np.array([1.2332, 4.7863, 5.3526, 7.3345])
print(np.fix(arr))

#Output:
[1. 4. 5. 7.]

floor():

The floor() function returns the floor of the input array, element-wise. The floor of a number say num is a largest integer x such that x < = num.

Syntax:

floor(x)

The x parameter is the input array.

arr = np.array([-1.2332, -4.7863, 5.3526, 2.3345])
print(np.floor(arr))

#Output:
[-2. -5.  5.  2.]

ceil():

The ceil() function returns the ceiling of the input array, element-wise. The ceiling is a process of making the decimal numbers to the nearest upper integer. For example, when we ceil 6.17, 6.82 the results will be 7.0, 7.0 respectively.

Syntax:

ceil(x)

The x parameter is the input array.

arr = np.array([-1.2332, -4.7863, 5.3526, 2.3345])
print(np.ceil(arr))

#Output:
[-1. -4.  6.  3.]

trunc():

The trunc() function of NumPy returns the truncated value of the input, element-wise. The truncated value of a number num is the nearest integer x which is closer to zero than num is, and the fractional part of the number num is discarded.

Syntax:

trunc(x)

The x parameter is the input array.

arr = np.array([-1.2332, -4.7863, 5.3526, 2.3345])
print(np.trunc(arr))

#Output:
[-1. -4.  5.  2.]

Some commonly used functions:

  • sum()
  • prod()
  • cumsum()
  • cumprod()
  • fmax()
  • fmin()
  • lcm()
  • gcd()
  • square()
  • sqrt()
  • cbrt()
  • exp()
  • log()

sum():

The NumPy sum() function calculates the sum of the input array elements over a given axis.

Syntax:

sum(a, axis=None, dtype=None)

The a parameter is the input array. The axis parameter is an axis or axes along which a sum is performed(can be integer value or tuple of integers). The dtype parameter is the datatype of the returned value.

arr = np.arange(1, 11,2.5).reshape(2,2)
print(np.sum(arr, axis=0, dtype = int))

#Output:
[ 7 11]

print(np.sum(arr, axis=1, dtype = float))

#Output:
[ 4.5 14.5]

prod():

The prod() function returns the product of array elements over a given axis.

Syntax:

prod(a, axis=None, dtype=None)
arr = np.arange(1, 11,2.5).reshape(2,2)
print(np.prod(arr, axis=0, dtype = int))

#Output:
[ 6 24]

print(np.prod(arr, axis=1, dtype = float))

#Output:
[ 3.5 51. ]

cumsum():

The cumsum() of NumPy is used to calculate the cumulative sum of the elements of the input array along a given axis.

Syntax:

cumsum(a, axis=None, dtype=None)
arr = np.arange(1, 11,2.5).reshape(2,2)
print(np.cumsum(arr, axis=0, dtype = float))

#Output:
[[ 1.   3.5]
 [ 7.  12. ]]

print(np.cumsum(arr, axis=1, dtype = int))

#Output:
[[ 1  4]
 [ 6 14]]

cumprod():

The cumprod() function returns the cumulative product of elements of the input array along a given axis.

Syntax:

cumprod(a, axis=None, dtype=None)
arr = np.arange(1, 11,2.5).reshape(2,2)
print(np.cumprod(arr, axis=1, dtype = float))

#Output:
[[ 1.   3.5]
 [ 6.  51. ]]

print(np.cumprod(arr, axis=0, dtype = int))

#Output:
[[ 1  3]
 [ 6 24]]

fmax():

The fmax() function of NumPy is used to get maximum values of the input array, element-wise. The fmax() function compares two arrays and returns a new array containing the element-wise maxima.

Syntax:

fmax(x1, x2)

The x1 and x2 parameters are the input arrays, and the shape of x1 and x2 should be the same or broadcastable to a common shape.

a = np.array([[0,2], [5, 7]])
b = np.array([[3, 1], [8, 6]])
print(np.fmax(a, b))

#Output:
[[3 2]
 [8 7]]

fmin():

The fmin() function calculates minimum values of the input array, element-wise. The fmin() function compares two arrays and returns a new array containing the element-wise minima.

Syntax:

fmin(x1, x2)
a = np.array([[0,2], [5, 7]])
b = np.array([[3, 1], [8, 6]])
print(np.fmin(a, b))

#Output:
[[0 1]
 [5 6]]

lcm():

The lcm() function is used to get the lowest common multiple of the two input arrays, element-wise.

Syntax:

lcm(x1, x2)
a = np.array([[0,2], [5, 7]])
b = np.array([[3, 1], [8, 6]])
print(np.lcm(a, b))

#Output:
[[ 0  2]
 [40 42]]

gcd():

The NumPy gcd() function calculates the greatest common divisor of the two input arrays, element-wise.

Syntax:

gcd(x1, x2)
a = np.array([[0,2], [5, 7]])
b = np.array([[3, 1], [8, 6]])
print(np.gcd(a, b))

#Output:
[[3 1]
 [1 1]] 

square():

The square() function returns the square values of the input array, element-wise.

Syntax:

square(x)

The x parameter is the input array.

arr = np.arange(10)
print(np.square(arr))

#Output:
[ 0  1  4  9 16 25 36 49 64 81]

sqrt():

The sqrt() function of NumPy calculates the non-negative square root of the input array, element-wise.

Syntax:

sqrt(x)

The x parameter is the input array.

arr = np.arange(10)
print(np.sqrt(arr))

#Output:
[0.         1.         1.41421356 1.73205081 2.         2.23606798
 2.44948974 2.64575131 2.82842712 3.        ]

cbrt():

The cbrt() function of NumPy calculates the cube root of the input array, element-wise.

Syntax:

cbrt(x)

The x parameter is the input array.

arr = np.arange(10)
print(np.cbrt(arr))

#Output:
[0.         1.         1.25992105 1.44224957 1.58740105 1.70997595
 1.81712059 1.91293118 2.         2.08008382]

exp():

The exp() function is used to get the exponential(e) of all elements in the input array.

Syntax:

exp(x)

The x parameter is the input array.

arr = np.arange(10)
print(np.exp(arr))

#Output:
[1.00000000e+00 2.71828183e+00 7.38905610e+00 2.00855369e+01
 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03
 2.98095799e+03 8.10308393e+03]

log():

The log() function of NumPy is used to calculate the natural logarithm of the input array, element-wise.

Syntax:

log(x)

The x parameter is the input array.

arr = np.arange(1, 10)
print(np.log(arr))

#Output:
[0.         0.69314718 1.09861229 1.38629436 1.60943791 1.79175947
 1.94591015 2.07944154 2.19722458]