Development

NumPy Ceil Function

Captain Salem 2 min read
NumPy Ceil Function

The NumPy ceil function allows you to determine the ceiling of each element in a given array. This allows you to get the smallest equivalent of a given array.

With a curious mind, let's dive in and explore how this function works and how we can use it.

Function Syntax

The function syntax is as shown in the code snippet below:

numpy.ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
  • x: array_like
    The input array containing the elements for which to compute the ceiling.
  • out: ndarray, None, or tuple of ndarray and None, optional
    A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided, a new array is returned.
  • where: array_like, optional
    This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result; otherwise, it will retain its original value.
  • casting: {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
    Controls what kind of data casting may occur.
  • order: {'C', 'F', 'A', 'K'}, optional
    Controls the memory layout order of the result.
  • dtype: data-type, optional
    Overrides the data type of the output array.
  • subok: bool, optional
    If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

The most important parameter you will need to know is x. This specifies the array whose elements ceiling values you wish to determine.

Examples – Ceiling Values of Integer Array

The code below shows how to use the ceil function to determine the ceiling values of a given input integer array.

>>> import numpy as np
>>> arr = np.array([[10,20,30,40,50],[60,70,80,90, 100]])
>>> print(np.ceil(arr))

Output:

[[ 10. 20. 30. 40. 50.]
[ 60. 70. 80. 90. 100.]]

The function will return the ceiling of the input integers as floating-point values.

Example 2 – Ceiling values of Floating-Point Array

In the example below, we provide an array of floating point values to the ceil function.

>>> import numpy as np
>>> arr = np.array([[10.11,20.34,30.56,40.74,50.55][60.96,70.34,80.03,90.31,100.99]])
>>> print(np.ceil(arr))

Output array:

[[ 11. 21. 31. 41. 51.]
[ 61. 71. 81. 91. 101.]]

From the output, we can verify that the function does indeed return the smallest value for every element in the input array, also known as the ceiling of an input.

Example 3 – Ceiling Values of Negative Floats

We can also specify an array containing negative values. An example is as illustrated below:

>>> import numpy as np
>>> arr = np.array([[-10.11,20.34,-30.56,40.74,-50.55][60.96,70.34,-80.03,90.31, -100.99]])
>>> print(np.ceil(arr))

The function should return output as:

[[ -10. 21. -30. 41. -50.]
[ 61. 71. -80. 91. -100.]]

Notice the difference between the positive floating values and the negative ones?

Termination

In this article, we explored various ways of using the NumPy ceil() function against various types of arrays.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.