跳到内容

std

计算输入数组 x 的标准差。

参数

名称 类型 描述 默认值
x

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

必需
axis

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

None
correction

自由度调整。将此参数设置为除 0 以外的值,会根据 N-c 调整标准差计算过程中的除数,其中 N 对应于计算标准差的元素总数,c 对应于提供的自由度调整。当计算总体的标准差时,将此参数设置为 0 是标准选择(即,提供的数组包含构成整个总体的所有数据)。当计算修正后的样本标准差时,将此参数设置为 1 是标准选择(即,提供的数组包含从更大总体中抽样的数据;这通常被称为贝塞尔校正)。默认值:0

0.0
keepdims

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

False

返回值

名称 类型 描述
out 数组

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

特殊情况

N 等于计算标准差的元素数量。

  • 如果 N - correction 小于或等于 0,则标准差为 NaN
  • 如果 x_iNaN,则标准差为 NaN(即,NaN 值会传播)。

示例

>>> a = sparse.COO.from_numpy(np.array([[0, 2], [-1, 1]]))
>>> o = sparse.std(a, axis=1)
>>> o.todense()
array([1., 1.])
源代码位于 sparse/numba_backend/_common.py
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
def std(x, /, *, axis=None, correction=0.0, keepdims=False):
    """
    Calculates the standard deviation 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 standard deviations are computed.
        By default, the standard deviation is computed over the entire array.
        If a tuple of integers, standard deviations are computed over multiple axes.
        Default: ``None``.
    correction: Union[int, float]
        degrees of freedom adjustment.
        Setting this parameter to a value other than ``0`` has the effect of adjusting the divisor
        during the calculation of the standard deviation according to ``N-c`` where ``N`` corresponds
        to the total number of elements over which the standard deviation is computed
        and ``c`` corresponds to the provided degrees of freedom adjustment. When computing the
        standard deviation of a population, setting this parameter to ``0`` is the standard choice
        (i.e., the provided array contains data constituting an entire population). When computing
        the corrected sample standard deviation, setting this parameter to ``1`` is the standard
        choice (i.e., the provided array contains data sampled from a larger population; this is
        commonly referred to as Bessel's correction). Default: ``0``.
    keepdims: bool
        if ``True``, the reduced axes (dimensions) are included in the result as singleton
        dimensions, and, accordingly, the result must be 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 standard deviation was computed over the entire array, a zero-dimensional array containing
        the standard deviation; otherwise, a non-zero-dimensional array containing the standard deviations.
        The returned array has the same data type as ``x``.

    Special Cases
    -------------
    Let ``N`` equal the number of elements over which to compute the standard deviation.

    -   If ``N - correction`` is less than or equal to ``0``, the standard deviation is ``NaN``.
    -   If ``x_i`` is ``NaN``, the standard deviation is ``NaN`` (i.e., ``NaN`` values propagate).

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