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

</>
Copy
ndarray.any(axis=None, out=None, keepdims=False, *, where=True)

Parameters

ParameterTypeDescription
axisNone, int, or tuple of ints, optionalAxis or axes along which a logical OR operation is performed. If None, it checks the entire array.
outndarray, optionalAlternative output array for storing the result. Must have the same shape as expected output.
keepdimsbool, optionalIf True, the reduced dimensions are kept as size one, allowing proper broadcasting.
wherearray_like of bool, optionalSpecifies 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.

</>
Copy
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.

</>
Copy
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.

</>
Copy
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.

</>
Copy
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.