Python NumPy ceil()
function is used to return the ceil values for each element of an input array (element-wise). This function takes two arguments arr
and out
and returns a new array with ciel values for the source array arr
. The ceil of the scalar x
is the smallest integer i
, such that i >= x
. In simple words, the ceil value is always greater than equal to the given value.
In this article, I will explain how to find the ceil values of elements of an input array, using the numpy.ceil()
function with examples.
1. Quick Examples of Python NumPy ceil() Function
If you are in a hurry, below are some quick examples of how to use Python NumPy ceil()
function.
# Below are the quick examples
# Example 1: Create single element array
arr = np.array([7.8])
arr1 = np.ceil(arr)
# Example 2: Create an array
arr = [.6, 5.5, 8.5, 3.5, 4.5, 11.1]
arr1 = np.ceil(arr)
# Example 3: Create an 1-D NumPy array
arr = np.array([-0.8, -4.1, -9.7, -8.0, -5, -6])
arr1 = np.ceil(arr)
# Example 4: Create 2-D array
arr = np.array([[0.8, 4.1, 9.7],[ 8.0, 5 ,6]])
arr1 = np.ceil(arr)
2. Syntax of NumPy ceil()
Following is the syntax of numpy.ceil()
.
# Syntax of ceil()
numpy.ceil(arr [, out]) = ufunc ‘ceil’)
2.1 Parameters of NumPy ceil ()
The ceil() function allows mainly two parameters:
arr
: The values whose ceil values are required. Input array.
out
: A ndarray the result is stored in. If given, it must have a shape of input arr
. If not given or None, it returns a freshly-allocated array. It also takes a tuple as input, if given a tuple must have a length equal to the number of outputs (possible only as a keyword argument).
2.2 Return Value of ceil()
The ceil()
function returns the ceil value of each array element and the elements in a returned array would be float
data type.
3. Usage of NumPy ceil() Function
The numpy.ceil()
is a mathematical function that returns the ceil value of an element array with float data type. The input array elements should have a real numbers and assume x, it rounds the variable in an upwards manner to the nearest integer, and finally returns the nearest integers as ceil values. If a value of x is an integer, it just returns the x value as-is.
The ciel()
function varies from another NumPy function floor() which is used to return the variable rounded downwards.
Below I have covered some examples to understand the concept of ceil()
. In order to find out the ceil values of a NumPy array first, we have to create the NumPy array using numpy.array()
.
4. Get the Single ceil Value of 1-Dimensional NumPy Array
To calculate the ceil values of 1-D array elements using numpy.ceil()
function.
# Import numpy module
import numpy as np
# Create single element array
arr = np.array([7.8])
arr1 = np.ceil(arr)
print(arr1)
# Output:
# [8.]
From the above code, we got a ceil value of the input array named arr
. That means 7.8 is our float element and its ceil value is 8. (i.e after the nearest integer)
5. Get the Multiple ceil Values of 1-D Array
Let’s get the ceil values of the input array with multiple elements. For example,
# Create an 1-D input array
arr = np.array([0.8, 4.1, 9.7, 8.0, 5, 6])
arr1 = np.ceil(arr)
print(arr1)
# Output:
# [ 1. 5. 10. 8. 5. 6.]
6. Get the ceil Values of Negative Elements
Let’s calculate the ceil value for negative values. Here I will be using the same as the above example but with negative values.
# Create an 1-D input array
# Use ceil() function
arr = np.array([-0.8, -4.1, -9.7, -8.0, -5, -6])
arr1 = np.ceil(arr)
print(arr1)
# Output :
# [-0. -4. -9. -8. -5. -6.]
From the above, as you can observe the ceil values of negative elements and positive elements of the same array are different.
7. Get the ceil() Value of the 2-D NumPy Array
Finally, Let’s use the ceil()
function for 2-dimensional arrays. Note that syntax doesn’t change for 1-D or 2-D.
# Create 2-D array
arr = np.array([[0.8, 4.1, 9.7],[ 8.0, 5 ,6]])
arr1 = np.ceil(arr)
print(arr1)
# Output:
# [[ 1. 5. 10.]
# [ 8. 5. 6.]]
8. Conclusion
In this article, I have explained how to use Python numpy.ceil()
function, and using this how to calculate the ceil values of all the array elements with examples.
Happy learning !!
Related Articles
- NumPy array operations
- How do I get the square root values of an array?
- Get NumPy Array Length
- How do I get the floor value of an array?
- How do I get the square value of an array?
- Python NumPy repeat() Function
- Python NumPy Absolute Value
- Python NumPy Split Array – Using split() Function
- Python NumPy Reverse Array