np.all: How to Use Numpy all() Function in Python
Data manipulation in Python is nearly identical with Numpy array manipulation: even newer libraries like Pandas are built around the NumPy array.
Numpy any() function checks whether all array elements along the mentioned axis evaluate True or False. If there are all elements in a particular axis, is True, it returns True.
The np.all() function returns True when all the elements of ndarray passed to the first parameter are True and returns False otherwise. The np.all() function takes four arguments in which one is required, and the other three are optional.
To test whether all array elements along the mentioned axis evaluate True in Python, use the np.all() method. 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 done a logical reduction of OR. The default (axis = None) executes logical OR overall input array dimensions. Axis may be negative, in which case it is 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 axis or all of the axes as before.
- 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 set to 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.
If the default value is passed, then keepdims will not be passed through to any method of sub-classes of ndarray. However, any non-default value will bAny exceptions will be raised if the sub-class’ method does not implement keepdims.
The all() function 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.
Program to show the working of any()
See the following code.
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]
In the first type example, we are testing all() column-wise, and we can see that in the first column, all the values are True, so the ans is True, and in the second column, all the values are False, so ans is False.
In the second type example, we can see the third value is 0, so as not all values are True, the answer is False.
In the third example, we have numpy.nan, as True; the answer is True.
In the fourth example, we have all the values that are 0, so our answer is False.
Difference between np.all() vs and.np.any()
The numpy.any() function returns True if at least one item in a Numpy array evaluates to True.
The numpy.all() function returns True only if all items in a Numpy array evaluate as True.
These tests can be performed considering the n-dimensional array as a flat array or over a specific axis of the array.