spectrochempy.average

average(dataset, dim=None, weights=None, keepdims=False)[source]

Compute the weighted average along the specified axis.

Parameters:
  • dataset (array_like) – Array containing data to be averaged.

  • dim (None, int, str, or tuple of int or str, optional) – Dimension or dimensions along which to average. By default, the flattened input is used. A tuple averages over multiple dimensions.

  • weights (array_like, optional) – An array of weights associated with the values in dataset. Each value contributes to the average according to its associated weight. The weights array can either be 1-D (in which case its length must be the size of dataset along the given axis) or of the same shape as dataset . If weights=None , then all data in dataset are assumed to have a weight equal to one. The 1-D calculation is:

    avg = sum(a * weights) / sum(weights)
    

    The only constraint on weights is that sum(weights) must not be 0.

Returns:

NDDataset, Quantity, or scalar – Weighted average along the selected dimension or dimensions. A full reduction returns a scalar or a quantity when units are defined.

Raises:
  • ZeroDivisionError – When all weights along axis are zero. See numpy.ma.average for a version robust to this type of error.

  • TypeError – When the length of 1D weights is not the same as the shape of a along axis.

See also

mean

Compute the arithmetic mean along the specified axis.

Examples

>>> nd = scp.read('irdata/nh4y-activation.spg')
>>> nd
NDDataset: [float64] a.u. (shape: (y:55, x:5549))
>>> scp.average(nd)
<Quantity(1.25085858, 'absorbance')>
>>> m = scp.average(nd, dim='y')
>>> m
NDDataset: [float64] a.u. (size: 5549)
>>> m.x
Coord: [float64] cm⁻¹ (size: 5549)
>>> m = scp.average(nd, dim='y', weights=scp.arange(55))
>>> m.data
array([   1.789,    1.789, ...,    1.222,     1.22])