Skip to content

clip

Clip (limit) the values in the array.

Return an array whose values are limited to [min, max]. One of min or max must be given.

Parameters:

Name Type Description Default
a
required
min scalar or `SparseArray` or `None`

Minimum value. If None, clipping is not performed on lower interval edge.

None
max scalar or `SparseArray` or `None`

Maximum value. If None, clipping is not performed on upper interval edge.

None
out SparseArray

If provided, the results will be placed in this array. It may be the input array for in-place clipping. out must be of the right shape to hold the output. Its type is preserved.

None

Returns:

Name Type Description
clipped_array SparseArray

An array with the elements of self, but where values < min are replaced with min, and those > max with max.

Examples:

>>> import sparse
>>> x = sparse.COO.from_numpy([0, 0, 0, 1, 2, 3])
>>> sparse.clip(x, min=1).todense()
array([1, 1, 1, 1, 2, 3])
>>> sparse.clip(x, max=1).todense()
array([0, 0, 0, 1, 1, 1])
>>> sparse.clip(x, min=1, max=2).todense()
array([1, 1, 1, 1, 2, 2])
See Also

numpy.clip : Equivalent NumPy function

Source code in sparse/numba_backend/_coo/common.py
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
def clip(a, min=None, max=None, out=None):
    """
    Clip (limit) the values in the array.

    Return an array whose values are limited to ``[min, max]``. One of min
    or max must be given.

    Parameters
    ----------
    a
    min : scalar or `SparseArray` or `None`
        Minimum value. If `None`, clipping is not performed on lower
        interval edge.
    max : scalar or `SparseArray` or `None`
        Maximum value. If `None`, clipping is not performed on upper
        interval edge.
    out : SparseArray, optional
        If provided, the results will be placed in this array. It may be
        the input array for in-place clipping. `out` must be of the right
        shape to hold the output. Its type is preserved.

    Returns
    -------
    clipped_array : SparseArray
        An array with the elements of `self`, but where values < `min` are
        replaced with `min`, and those > `max` with `max`.

    Examples
    --------
    >>> import sparse
    >>> x = sparse.COO.from_numpy([0, 0, 0, 1, 2, 3])
    >>> sparse.clip(x, min=1).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([1, 1, 1, 1, 2, 3])
    >>> sparse.clip(x, max=1).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([0, 0, 0, 1, 1, 1])
    >>> sparse.clip(x, min=1, max=2).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([1, 1, 1, 1, 2, 2])

    See Also
    --------
    numpy.clip : Equivalent NumPy function
    """
    a = asCOO(a, name="clip")
    return a.clip(min, max)