NumPy np.ceil()

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:

numpy.ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'ceil'>

Function Parameters

The parameters to which you need to pay attention are as discussed below:

  1. x – refers to the input array or an array_like object
  2. out – specifies a location to store the result of the function.
  3. casting – determines how the data is to be cast.
  4. 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.

Example 1

The code example demonstrates how to use the ceil function to a one-dimensional array containing floating-point values.

# import numpy
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:

[ 21. 11. -0. -3.]

Example 2

When applying the ceil() function to a multi-dimensional array, the functionality does not change.

Take the example code shown below:

arr_2d = np.array([[3.14159, -3.14159], [-.14159, .14159]])

This should return:

[[ 4. -3.]
[-0. 1.]]


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!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list