NumPy ndarray.any()
The numpy.ndarray.any()
method checks whether any element in a NumPy array evaluates to True
.
It can operate across the entire array or along a specified axis.
Syntax
ndarray.any(axis=None, out=None, keepdims=False, *, where=True)
Parameters
Parameter | Type | Description |
---|---|---|
axis | None, int, or tuple of ints, optional | Axis or axes along which a logical OR operation is performed. If None , it checks the entire array. |
out | ndarray, optional | Alternative output array for storing the result. Must have the same shape as expected output. |
keepdims | bool, optional | If True , the reduced dimensions are kept as size one, allowing proper broadcasting. |
where | array_like of bool, optional | Specifies elements to include in the check for at least one True value. |
Return Value
Returns a boolean value if axis=None
, or an array of boolean values if an axis is specified.
The result is True
if at least one element (or one element along the specified axis) evaluates to True
, otherwise False
.
Examples
1. Checking if Any Element is True in ndarray
In this example, we create a 2×2 boolean array and check if at least one of its elements is True
.
import numpy as np
arr = np.array([[False, False],
[False, True]])
result = arr.any()
print(result)
Output:
True
Since there is at least one True
value in the array, the any()
method returns True
.
2. Using the axis
Parameter in ndarray.any()
Here, we check if at least one element along a specific axis is True
.
import numpy as np
arr = np.array([[False, True],
[False, False]])
result_axis0 = arr.any(axis=0)
print(result_axis0)
result_axis1 = arr.any(axis=1)
print(result_axis1)
Output:
[False True]
[ True False]
For axis=0
(columns), it checks if at least one element in each column is True
. The second column contains a True
value, so the result is [False, True]
.
For axis=1
(rows), it checks if at least one element in each row is True
. The first row contains a True
value, so it returns True
, but the second row contains only False
values, so it returns False
.
3. Keeping Dimensions with keepdims=True
in ndarray.any()
In this example, we use keepdims=True
to retain the reduced axis as a dimension of size one.
import numpy as np
arr = np.array([[False, True],
[True, False]])
result = arr.any(axis=1, keepdims=True)
print(result)
Output:
[[ True]
[ True]]
Even though at least one element is True
in each row, keeping dimensions ensures that the output retains the original shape, making it useful for broadcasting operations.
4. Using the where
Parameter in ndarray.any()
The where
parameter allows checking only specific elements for at least one True
value.
import numpy as np
arr = np.array([False, False, True])
mask = np.array([False, False, True])
result = arr.any(where=mask)
print(result)
Output:
True
Here, only the elements where mask
is True
are considered. Since the last element is True
and is included in the mask, the result is True
, even though the first two elements are False
.