Numpy Ceil Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to use the numpy.ceil() function in the Numpy library. The numpy.ceil() function is used to return the ceil of the elements of an array. The ceil value of any **scalar valuex ** is the smallest **integeri in a way ** such that i >= x . In simpler words we can say, the nearest larger integer value is the ceil value.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) numpy(("`NumPy`")) -.-> numpy/MathandStatisticsGroup(["`Math and Statistics`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") numpy/MathandStatisticsGroup -.-> numpy/rand_num("`Random Numbers`") subgraph Lab Skills python/lists -.-> lab-86414{{"`Numpy Ceil Function`"}} python/importing_modules -.-> lab-86414{{"`Numpy Ceil Function`"}} python/standard_libraries -.-> lab-86414{{"`Numpy Ceil Function`"}} python/math_random -.-> lab-86414{{"`Numpy Ceil Function`"}} python/numerical_computing -.-> lab-86414{{"`Numpy Ceil Function`"}} python/build_in_functions -.-> lab-86414{{"`Numpy Ceil Function`"}} numpy/bool_idx -.-> lab-86414{{"`Numpy Ceil Function`"}} numpy/fancy_idx -.-> lab-86414{{"`Numpy Ceil Function`"}} numpy/rand_num -.-> lab-86414{{"`Numpy Ceil Function`"}} end

Import Numpy Library

Before using the numpy.ceil() function, we need to import Numpy library. Use the following code snippet to import numpy:

import numpy as np

Positive Values Example

In this example, we'll create an array with positive values and apply the numpy.ceil() function on it.

a = [1.90,2.3,0.6788]
y = np.ceil(a)
print("the output after applying ceil() is:")
print(y)

Output:

the output after applying ceil() is:
[2. 3. 1.]

Negative Values Example

In this example, we'll create an array with negative values and apply the numpy.ceil() function on it. Notice that when we find the ceil value for a negative number, then the larger integer number for let's say -1.9 will not be -2 , but it will be -1.

a = [-1.90,-2.3,-0.6788,12.34]
y = np.ceil(a)
print("the output after applying ceil() is:")
print(y)

Output:

the output after applying ceil() is:
[-1. -2. -0. 13.]

Large Array Example

In this example, we'll create a large array and apply the numpy.ceil() function on it.

a = np.random.rand(10)*10
y = np.ceil(a)
print("Original Array: ")
print(a)
print("the output after applying ceil() is:")
print(y)

Output:

Original Array:
[3.28429956 5.4691611  2.30726608 9.89976363 9.89927599 7.34843176
 2.20207494 5.91541854 2.93295467 2.26747722]
the output after applying ceil() is:
[ 4.  6.  3. 10. 10.  8.  3.  6.  3.  3.]

Non-Float Array Example

In this example, we'll create a non-float array and apply the numpy.ceil() function on it.

a = [1, 2, 3, 4, 5]
y = np.ceil(a)
print("the output after applying ceil() is:")
print(y)

Output:

the output after applying ceil() is:
[1. 2. 3. 4. 5.]

Summary

In this lab, you learned how to use the numpy.ceil() function in the Numpy library. We covered its syntax, parameters as well as the value returned by this function along with a few useful examples to help you understand how to use this function. The numpy.ceil() function can be used to find the smallest integer greater than or equal to the input array values.

Other Python Tutorials you may like