Numpy – all() – Check if All Elements are True

Numpy – all()

Numpy all() function checks if all elements in the array, along a given axis, evaluate to True.

If all elements evaluate to True, then all() returns True, else all() returns False.

Example 1: all()

In this example, we will take a Numpy Array with all its elements as True. We will pass this array as argument to all() function. The function should return True, since all the elements of array evaluate to True.

Python Program

import numpy as np

arr = np.array([[True,True],[True,True]])
result = np.all(arr)
print(result)
Run

Output

True

Example 2: all() – Some Elements are False

In this example, we will take a Numpy Array with some of its elements as False. We will pass this array as argument to all() function. The function should return False, since all the values of the given array does not evaluate to True.

Python Program

import numpy as np

arr = np.array([[True,True],[False,False]])
result = np.all(arr)
print(result)
Run

Output

False

Example 3: all() – Along an Axis

In this example, we will take a Numpy Array with boolean values. We will check if all the elements of the array are True along specified axis using numpy.all() function.

Python Program

import numpy as np

arr = np.array([[True,True], [True,False], [True,False]])
result = np.all(arr, axis=1)
print(result)
Run

Output

[ True False False]

Explanation

numpy.all() along axis 1

Now, we shall apply the function all() along axis=0.

Python Program

import numpy as np

arr = np.array([[True,True], [True,False], [True,False]])
result = np.all(arr, axis=0)
print(result)
Run

Output

[ True False]

Explanation

numpy.all() along axis 0

Summary

In this tutorial of Python Examples, we learned how to use numpy.all() function to check if all elements are True, along an axis if given, with the help of example programs.