Numpy.all() method is “used to test whether all array elements along the mentioned axis evaluate to True.” If you specify the parameter axis, it returns True if all elements are True for each axis.
numpy.all(array, axis = None, out = None, keepdims = <NoValue>)
The all() function takes up to four parameters.
- array: This is the array on which we need to work.
- axis: Axis or axes around, which is a logical reduction of OR. The default (axis = None) executes logical OR overall input array dimensions. Axis may be negative, counted from the last axis to the first. If this is a tuple of ints, there is a reduction on multiple axes instead of a single or all axes.
- out: This is an optional field. Alternate output array to position the result into. It must have the same shape as the planned performance and maintain its form.
- keepdims: If this is True, the reduced axes are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
The np.all() method always returns a Boolean value. But this boolean value depends on the ‘out’ parameter.
Please note that Not a Number (NaN), positive infinity, and negative infinity are evaluated to True as they are not equal to zero.
Example 1: How to Use numpy.all() Method
import numpy as np #Declaring different types of array arr1 = [[True, False], [True, False]] print(np.all(arr1, axis=0)) arr2 = [5, 10, 0, 100] print(np.all(arr2)) print(np.all(np.nan)) arr3 = [[0, 0], [0, 0]] print(np.all(arr3, axis=0))
[ True False] False True [False False]
Example 2: Using the numpy all() method with a multidimensional array
import numpy as np # Create a 2D array arr = np.array([[True, True], [True, True]]) # Use numpy.all() without specifying an axis result = np.all(arr) print(result)