跳到内容

均值

计算输入数组 x 的算术平均值。

参数

名称 类型 描述 默认值
x

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

必需
axis

必须计算算术平均值的轴。默认情况下,均值是在整个数组上计算的。如果是整数元组,则在多个轴上计算算术平均值。默认值:None

None
keepdims

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

False

返回值

名称 类型 描述
out 数组

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

特殊情况

N 等于计算算术平均值的元素数量。如果 N0,则算术平均值为 NaN。如果 x_iNaN,则算术平均值为 NaN(即 NaN 值会传播)。

示例

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.mean(a, axis=1)
>>> o.todense()
array([0.5, 1. ])
源代码位于 sparse/numba_backend/_common.py
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
def mean(x, /, *, axis=None, keepdims=False, dtype=None):
    """
    Calculates the arithmetic mean of the input array ``x``.

    Parameters
    ----------
    x: array
        input array of  a real-valued floating-point data type.
    axis: Optional[Union[int, Tuple[int, ...]]]
        axis or axes along which arithmetic means must be computed.
        By default, the mean is computed over the entire array.
        If a tuple of integers, arithmetic means 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 is the input array.
        Otherwise, if ``False``, the reduced axes (dimensions) are not be included in the result. Default: ``False``.

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

    Special Cases
    -------------
    Let ``N`` equal the number of elements over which to compute the arithmetic mean.
    If ``N`` is ``0``, the arithmetic mean is ``NaN``.
    If ``x_i`` is ``NaN``, the arithmetic mean is ``NaN`` (i.e., ``NaN`` values propagate).

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.mean(a, axis=1)
    >>> o.todense()
    array([0.5, 1. ])
    """

    return x.mean(axis=axis, keepdims=keepdims, dtype=dtype)