Warning

You are reading the documentation related to the development version. Go here if you are looking for the documentation of the stable release.

spectrochempy.var

var(dataset, dim=None, dtype=None, ddof=0, keepdims=False)[source]

Compute the variance along the specified axis.

Returns the variance of the array elements, a measure of the spread of a distribution. The variance is computed for the flattened array by default, otherwise over the specified axis.

Parameters
  • dataset (array_like) – Array containing numbers whose variance is desired.

  • dim (None or int or dimension name , optional) – Dimension or dimensions along which to operate. By default, flattened input is used.

  • dtype (dtype, optional) – Type to use in computing the standard deviation. For arrays of integer type the default is float64, for arrays of float types it is the same as the array type.

  • ddof (int, optional) – Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof , where N represents the number of elements. By default ddof is zero.

  • keepdims (bool, optional) – If this is set to True, the dimensions 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

var – A new array containing the standard deviation.

See also

std

Standard deviation values along axis.

mean

Compute the arithmetic mean along the specified axis.

Notes

The variance is the average of the squared deviations from the mean, i.e., var = mean(abs(x - x.mean())**2) .

The mean is normally calculated as x.sum() / N , where N = len(x) . If, however, ddof is specified, the divisor N - ddof is used instead. In standard statistical practice, ddof=1 provides an unbiased estimator of the variance of a hypothetical infinite population. ddof=0 provides a maximum likelihood estimate of the variance for normally distributed variables.

Note that for complex numbers, the absolute value is taken before squaring, so that the result is always real and nonnegative.

For floating-point input, the variance is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 (see example below). Specifying a higher-accuracy accumulator using the dtype keyword can alleviate this issue.

Examples

>>> nd = scp.read('irdata/nh4y-activation.spg')
>>> nd
NDDataset: [float64] a.u. (shape: (y:55, x:5549))
>>> scp.var(nd)
<Quantity(0.652818786, 'absorbance')>
>>> scp.var(nd, keepdims=True)
NDDataset: [float64] a.u. (shape: (y:1, x:1))
>>> m = scp.var(nd, dim='y')
>>> m
NDDataset: [float64] a.u. (size: 5549)
>>> m.data
array([0.007262, 0.007299, ...,  0.06298,  0.06438])