Skip to main content
`Ctrl`+`K`
scipy.stats.

# variation

scipy.stats.variation(a, axis=0, nan_policy='propagate', ddof=0, *, keepdims=False)

  Compute the coefficient of variation.

  The coefficient of variation is the standard deviation divided by the mean. This function is equivalent to:

      np.std(x, axis=axis, ddof=ddof) / np.mean(x)

  The default for `ddof` is 0, but many definitions of the coefficient of variation use the square root of the unbiased sample variance for the sample standard deviation, which corresponds to `ddof=1`.

  The function does not take the absolute value of the mean of the data, so the return value is negative if the mean is negative.

  Parameters:
    a array_like

      Input array.

    axis int or None, default: 0

      If an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If `None`, the input will be raveled before computing the statistic.

    nan_policy{‘propagate’, ‘omit’, ‘raise’}

      Defines how to handle input NaNs.

      - `propagate`: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.

      - `omit`: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.

      - `raise`: if a NaN is present, a `ValueError` will be raised.

    ddof int, optional

      Gives the “Delta Degrees Of Freedom” used when computing the standard deviation. The divisor used in the calculation of the standard deviation is `N - ddof`, where `N` is the number of elements. ddof must be less than `N`; if it isn’t, the result will be `nan` or `inf`, depending on `N` and the values in the array. By default ddof is zero for backwards compatibility, but it is recommended to use `ddof=1` to ensure that the sample standard deviation is computed as the square root of the unbiased sample variance.

    keepdims bool, default: False

      If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  Returns:
    variation ndarray

      The calculated variation along the requested axis.

  Notes

  There are several edge cases that are handled without generating a warning:

  - If both the mean and the standard deviation are zero, `nan` is returned.

  - If the mean is zero and the standard deviation is nonzero, `inf` is returned.

  - If the input has length zero (either because the array has zero length, or all the input values are `nan` and `nan_policy` is `'omit'`), `nan` is returned.

  - If the input contains `inf`, `nan` is returned.

  Beginning in SciPy 1.9, `np.matrix` inputs (not recommended for new code) are converted to `np.ndarray` before the calculation is performed. In this case, the output will be a scalar or `np.ndarray` of appropriate shape rather than a 2D `np.matrix`. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar or `np.ndarray` rather than a masked array with `mask=False`.

  References

  [1]

  Zwillinger, D. and Kokoska, S. (2000). CRC Standard Probability and Statistics Tables and Formulae. Chapman & Hall: New York. 2000.

  Examples

      >>> import numpy as np
      >>> from scipy.stats import variation
      >>> variation([1, 2, 3, 4, 5], ddof=1)
      0.5270462766947299

  Compute the variation along a given dimension of an array that contains a few `nan` values:

      >>> x = np.array([[  10.0, np.nan, 11.0, 19.0, 23.0, 29.0, 98.0],
      ...               [  29.0,   30.0, 32.0, 33.0, 35.0, 56.0, 57.0],
      ...               [np.nan, np.nan, 12.0, 13.0, 16.0, 16.0, 17.0]])
      >>> variation(x, axis=1, ddof=1, nan_policy='omit')
      array([1.05109361, 0.31428986, 0.146483  ])