How to get the floor, ceiling and truncated values of the elements of a numpy array?
Last Updated : 29 Aug, 2020
In this article, let's discuss how to get the floor, ceiling, and truncated values of the elements of a Numpy array. First, we need to import the NumPy library to use all the functions available in it. This can be done with this import statement:
import numpy as np
Getting the floor value
The greatest integer that is less than or equal to x where x is the array element is known as floor value. It can found using the function numpy.floor()
Syntax:
numpy.floor(x[, out]) = ufunc ‘floor’)
Example 1:
Python # Import the numpy library import numpy as np # Initialize numpy array a = np.array([1.2]) # Get floor value a = np.floor(a) print(a)
Output:
[1.]
Example 2:
Python import numpy as np a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0]) a = np.floor(a) print(a)
OutPut:
[-2., -2., -1., 0., 1., 1., 3.]
Getting the ceil value
The least integer that is greater than or equal to x where x is the array element is known as ceil value. It can be found using the numpy.ceil() method.
Syntax:
numpy.ceil(x[, out]) = ufunc ‘ceil’)
Example 1:
Python # Import the numpy library import numpy as np # Initialize numpy array a = np.array([1.2]) # Get ceil value a = np.ceil(a) print(a)
Output:
[2.]
Example 2:
Python import numpy as np a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0]) a = np.ceil(a) print(a)
Output:
[-1., -1., -0., 1., 2., 2., 3.]
Getting the Truncate value
The trunc of the scalar x is the nearest integer i which, closer to zero than x. This simply means that, the fractional part of the signed number x is discarded by this function. It can be found using the numpy.trunc() method.
Syntax:
numpy.trunc(x[, out]) = ufunc ‘trunc’)
Example 1:
Python # Import the numpy library import numpy as np # Initialize numpy array a = np.array([1.2]) # Get truncate value a = np.trunc(a) print(a)
Output:
[1.]
Example 2:
Python import numpy as np a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0]) a = np.trunc(a) print(a)
Output:
[-1., -1., -0., 0., 1., 1., 3.]
Example to get floor, ceil, trunc values of the elements of a numpy array
Python import numpy as np input_arr = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0]) print(input_arr) floor_values = np.floor(input_arr) print("\nFloor values : \n", floor_values) ceil_values = np.ceil(input_arr) print("\nCeil values : \n", ceil_values) trunc_values = np.trunc(input_arr) print("\nTruncated values : \n", trunc_values)
Output:
[-1.8 -1.6 -0.5 0.5 1.6 1.8 3. ] Floor values : [-2. -2. -1. 0. 1. 1. 3.] Ceil values : [-1. -1. -0. 1. 2. 2. 3.] Truncated values : [-1. -1. -0. 0. 1. 1. 3.]
Similar Reads
How to get element-wise true division of an array using Numpy? True Division in Python3 returns a floating result containing the remainder of the division. To get the true division of an array, NumPy library has a function numpy.true_divide(x1, x2). This function gives us the value of true division done on the arrays passed in the function. To get the element-w
2 min read
How to get the n-largest values of an array using NumPy? Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, ax
2 min read
How to calculate the element-wise absolute value of NumPy array? Let's see the program for finding the element-wise absolute value of NumPy array. For doing this task we are using numpy.absolute() function of NumPy library. This mathematical function helps to calculate the absolute value of each element in the array. Syntax: numpy.absolute(arr, out = None, ufunc
2 min read
How to round elements of the NumPy array to the nearest integer? Prerequisites: Python NumPy In this article, let's discuss how to round elements of the NumPy array to the nearest integer. numpy.rint() function of Python that can convert the elements of an array to the nearest integer. Syntax: numpy.rint(x, /, out=None, *, where=True, casting='same_kind', order='
1 min read
How to get values of an NumPy array at certain index positions? Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t
4 min read