跳到内容

max

计算输入数组 x 的最大值。

参数

名称 类型 描述 默认值
x

实值数据类型的输入数组。

必需
axis

计算最大值的轴。默认情况下,最大值在整个数组上计算。如果是一个整数元组,则在多个轴上计算最大值。默认值:None

keepdims

如果为 True,则将约简的轴(维度)作为单例维度包含在结果中。因此,结果与输入数组兼容。否则,如果为 False,则约简的轴(维度)不得包含在结果中。默认值:False

False

返回值

名称 类型 描述
out 数组

如果最大值是在整个数组上计算的,则为一个包含最大值的零维数组。否则,为一个包含最大值的非零维数组。返回的数组具有与 x 相同的数据类型。

特殊情况

对于浮点操作数,如果 x_iNaN,则最大值为 NaN(即,NaN 值会传播)。

示例

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.max(a, axis=1)
>>> o.todense()
array([1, 2])
源代码位于 sparse/numba_backend/_common.py
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
def max(x, /, *, axis=None, keepdims=False):
    """
    Calculates the maximum value of the input array ``x``.

    Parameters
    ----------
    x: array
        input array of a real-valued data type.
    axis: Optional[Union[int, Tuple[int, ...]]]
        axis or axes along which maximum values are computed.
        By default, the maximum value are computed over the entire array.
        If a tuple of integers, maximum values are computed over multiple axes. Default: ``None``.
    keepdims: bool
        If ``True``, the reduced axes (dimensions) are included in the result as singleton dimensions.
        Accordingly, the result is compatible with the input array.
        Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result. Default: ``False``.

    Returns
    -------
    out: array
        if the maximum value was computed over the entire array, a zero-dimensional array containing the maximum value.
        Otherwise, a non-zero-dimensional array containing the maximum values.
        The returned array has the same data type as ``x``.

    Special Cases
    -------------
    For floating-point operands, if ``x_i`` is ``NaN``, the maximum value is ``NaN`` (i.e., ``NaN`` values propagate).

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.max(a, axis=1)
    >>> o.todense()
    array([1, 2])
    """
    return x.max(axis=axis, keepdims=keepdims)