The ceil() function in NumPy allows you to get the ceiling of each element in an array. The ceiling of a number refers to the closest integer greater than or equal to the provided value.
Let us discover this function.
NumPy ceil() Function Syntax
In spite of the function’s simplistic functionality, it offers a lot of the parameters and options you can pass.
The syntax is expressed as shown below:
The parameters to which you need to pay attention are as discussed below:
- x – refers to the input array or an array_like object
- out – specifies a location to store the result of the function.
- casting – determines how the data is to be cast.
- dtype – defines the data type of the output array.
Function Return Value
The function returns an array populated with the ceil values of each element in the input array.
The resulting values will also hold scalar values if the input array contains scalar values.
The code example demonstrates how to use the ceil function to a one-dimensional array containing floating-point values.
import numpy as np
arr = np.array([1.2, .3, 10.3, -.4, -3.5])
In the code example above, we start by importing numpy and giving it the alias of np.
We then create a one-dimensional array using the np.array function. Finally, we calculate the ceil of each element in the array using the np.ceil() function.
This should return an array as shown:
When applying the ceil() function to a multi-dimensional array, the functionality does not change.
Take the example code shown below:
This should return:
This tutorial covers the ceil function in NumPy and how to use it to calculate the ceil value of each element in an input array.
Thanks for reading, and stay tuned!!