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.
-
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:
# Example program that uses numpy.all() # Create an NDArray # Fill in the array with random Boolean values print("The ndarray elements:") # Reduce through logical AND - row based # Reduce through logical AND - column based |
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:
# Example Python program that reduces the whole ndarray # Create an NDArray # Fill in the array print("Array elements:") # Reduce the whole array using logical AND # The result will be True only when all the elements of the |
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 |