跳到内容

var

计算输入数组 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.var(a, axis=1)
>>> o.todense()
array([1., 1.])
源代码位于 sparse/numba_backend/_common.py
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
def var(x, /, *, axis=None, correction=0.0, keepdims=False):
    """
    Calculates the variance 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 variances are computed.
        By default, the variance is computed over the entire array.
        If a tuple of integers, variances 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 variance according to ``N-c``
        where ``N`` corresponds to the total number of elements over which the variance is computed and ``c``
        corresponds to the provided degrees of freedom adjustment.
        When computing the variance 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 unbiased sample variance, 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 are included in the result as singleton dimensions, and,
        accordingly, the result is compatible with the input array.
        Otherwise, if ``False``, the reduced axes (dimensions) are not included in the result. Default: ``False``.

    Returns
    -------
    out: array
        if the variance was computed over the entire array, a zero-dimensional array containing the variance;
        otherwise, a non-zero-dimensional array containing the variances.
        The returned array must have the same data type as ``x``.

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

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

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