Skip to content

any

Tests whether any input array element evaluates to True along a specified axis.

Parameters:

Name Type Description Default
x

input array.

required
axis

axis or axes along which to perform a logical OR reduction. By default, a logical OR reduction is performed over the entire array. If a tuple of integers, logical OR reductions are performed over multiple axes. A valid axis must be an integer on the interval [-N, N), where N is the rank (number of dimensions) of x. If an axis is specified as a negative integer, the function determines the axis along which to perform a reduction by counting backward from the last dimension (where -1 refers to the last dimension). If provided an invalid axis, the function raises an exception. Default: None.

None
keepdims

If True, the reduced axes (dimensions) are included in the result as singleton dimensions, and, accordingly, the result must is compatible with the input array. Otherwise, if False, the reduced axes (dimensions) is not included in the result. Default: False.

False

Returns:

Name Type Description
out array

if a logical OR reduction was performed over the entire array, the returned array is a zero-dimensional array containing the test result. Otherwise, the returned array is a non-zero-dimensional array containing the test results. The returned array is of type bool.

Special Cases
  • Positive infinity, negative infinity, and NaN evaluate to True.

  • If x has a complex floating-point data type, elements having a non-zero component (real or imaginary) evaluate to True.

  • If x is an empty array or the size of the axis (dimension) along which to evaluate elements is zero, the test result is False.

Examples:

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.any(a, axis=1)
>>> o.todense()
array([ True, True])
Source code in sparse/numba_backend/_common.py
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
def any(x, /, *, axis=None, keepdims=False):
    """
    Tests whether any input array element evaluates to ``True`` along a specified axis.

    Parameters
    ----------
    x: array
        input array.
    axis: Optional[Union[int, Tuple[int, ...]]]
        axis or axes along which to perform a logical OR reduction.
        By default, a logical OR reduction is performed over the entire array.
        If a tuple of integers, logical OR reductions are performed over multiple axes.
        A valid ``axis`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of
        dimensions) of ``x``. If an ``axis`` is specified as a negative integer, the function determines
        the axis along which to perform a reduction by counting backward from the last dimension (where
        ``-1`` refers to the last dimension).
        If provided an invalid ``axis``, the function raises an exception.
        Default: ``None``.
    keepdims: bool
        If ``True``, the reduced axes (dimensions) are included in the result as singleton dimensions,
        and, accordingly, the result must is compatible with the input array. Otherwise, if ``False``,
        the reduced axes (dimensions) is not included in the result.
        Default: ``False``.

    Returns
    -------
    out: array
        if a logical OR reduction was performed over the entire array, the returned array is a
        zero-dimensional array containing the test result.
        Otherwise, the returned array is a non-zero-dimensional array containing the test results.
        The returned array is of type ``bool``.

    Special Cases
    -------------

       - Positive infinity, negative infinity, and NaN  evaluate to ``True``.

       - If ``x`` has a complex floating-point data type, elements having a non-zero component
        (real or imaginary) evaluate to ``True``.

       - If ``x`` is an empty array or the size of the axis (dimension) along which to evaluate elements
         is zero, the test result is ``False``.

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.any(a, axis=1)
    >>> o.todense()  # doctest: +NORMALIZE_WHITESPACE
    array([ True, True])
    """
    return x.any(axis=axis, keepdims=keepdims)