The all() function of numpy

The all() function of NumPy module condenses the columns and rows of an ndarray by applying logical AND operation. When axis=0, logical AND is applied column-wise; When axis=1, AND is applied row-wise.

Logical Operations Updated 19 Oct 2024 2 min read

Overview

  • The numpy.all() function reduces an NumPy array-like either column-wise or row-wise using logical AND.
  • When the value of the axis parameter is 0 the reduction is column-wise. When it is 1 the reduction is row-wise. When axis = None the entire array is reduced to a Boolean value and returned as given by the [Example 2](#Reducing the whole ndarray using logical AND).
  • The behaviour of numpy.all() is in contrast to the numpy.any() function. The numpy.any() returns True, when any of the elements are True for a given axis or the entire array. The all() and any() methods of numpy.ndarray provides Python code examples that compares and contrasts the ndarray equivalent of the numpy.all() and numpy.any() functions.

Example 1

python
# Example program that uses numpy.all()

            # to get the Boolean AND results of the ndarray

            # elements - row-wise (axis = 1), column-wise (axis = 0)

            import numpy

            from random import randrange

# Create an NDArray

            n1 = numpy.ndarray(shape = (4, 3), dtype = numpy.int32)

# Fill in the array with random Boolean values

            for i in range(0, 4):

                for j in range(0, 3):

                    n1[i][j] = randrange(0, 2)

print("The ndarray elements:")

            print(n1)

            print(n1.shape)

# Reduce through logical AND - row based

            print("Logical AND results when axis = 1:")

            x = numpy.all(n1, 1)

            print(x)

            print(x.shape)

# Reduce through logical AND - column based

            print("Logical AND results when axis = 0:")

            x = numpy.all(n1, 0)

            print(x)

            print(x.shape)

Output

Output
The ndarray elements:

[[0 1 0]

 [0 1 0]

 [1 1 0]

 [0 1 0]]

(4, 3)

Logical AND results when axis = 1:

[False False False False]

(4,)

Logical AND results when axis = 0:

[False  True False]

(3,)

##

Example 2 - Reducing the whole ndarray using logical AND

python
# Example Python program that reduces the whole ndarray

            # by applying logical AND on its elements - (axis = None)

            import numpy

            from random import randrange

# Create an NDArray

            n1 = numpy.ndarray(shape = (3, 3), dtype = numpy.int32)

# Fill in the array

            for i in range(0, 3):

                for j in range(0, 3):

                    n1[i][j] = randrange(0, 2)

print("Array elements:")

            print(n1)

            print(n1.shape)

# Reduce the whole array using logical AND

            print("Logical AND results when axis = None:")

            result = numpy.all(n1)

            print(result)

            print(type(result))

# The result will be True only when all the elements of the

            # ndarray evaluate to True

            n1[:,:] = 1

            print("The ndarray with all ones and axis = None:")

            print(n1)

            print("Result:")

            result = numpy.all(n1)

            print(result)

Output

Output
Array elements:

[[1 1 0]

 [0 1 1]

 [0 0 1]]

(3, 3)

Logical AND results when axis = None:

False

<class 'numpy.bool'>

The ndarray with all ones and axis = None:

[[1 1 1]

 [1 1 1]

 [1 1 1]]

Result:

True