Numpy ceil – Python NumPy ceil() Function

NumPy ceil() Function:

Numpy ceil: The ceil() function of the NumPy module returns the ceiling value of the given input array or data. The ceiling of the scalar x is the smallest integer “i”, such that i >= x.

Syntax:

numpy.ceil(x, out=None)

Parameters

x: This is required. It is an array (array-like) having elements for which the ceiling values are calculated.

out: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value: 

NP ceil: The ceiling value of each element of x is returned.

NumPy ceil() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above given array as an argument to the ceil() function of the numpy module to get the ceiling values of the given array elements
  • Store it in another variable.
  • Print the ceiling values of the given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([20.3, 5.5, 8.8, 30.076, 12.123])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array as an argument to the ceil() function of the 
# numpy module to get the ceiling values of the given array elements
# Store it in another variable.
ceil_vals = np.ceil(gvn_arry)
# Print the ceiling values of the given array elements
print("The ceiling values of the given array elements:")
print(ceil_vals)

Output:

The above given array is:
[20.3  5.5  8.8  30.076  12.123]
The ceiling values of the given array elements:
[21.  6.  9.  31.  13.]

Example2: Negative values are given for the Array

np.ceil: Here, we give the array which includes the elements with negative values.

For example, Let the number given = -1.7

The ceiling value for -1.7 = -1

When we calculate the ceiling value for a given negative number, the larger integer number, like -1.7, will be -1 rather than -2. Because -1 is a higher number than -1.7, and -2 is a smaller number than -1.7

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list of negative values as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the ceil() function of the numpy module to get the ceiling values of the given array elements with negative values.
  • Store it in another variable.
  • Print the ceiling values of the given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list of negative values as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([-20.3, -1.5, -2.8, -3, -12.6])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array as an argument to the ceil() function of the 
# numpy module to get the ceiling values of the given array elements
# Store it in another variable.
ceil_vals = np.ceil(gvn_arry)
# Print the ceiling values of the given array elements
print("The ceiling values of the given array elements:")
print(ceil_vals)

Output:

The above given array is:
[-20.3 -1.5 -2.8 -3. -12.6]
The ceiling values of the given array elements:
[-20. -1. -2. -3. -12.]