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 or int or dimension name or tuple of int or dimensions, optional) – Dimension or dimensions along which to operate. By default, flattened input is used. If this is a tuple, the minimum is selected over multiple dimensions, instead of a single dimension or all the dimensions as before.
weights (array_like, optional) – An array of weights associated with the values in
dataset
. Each value ina
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 ofdataset
along the given axis) or of the same shape asdataset
. Ifweights=None
, then all data indataset
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 thatsum(weights)
must not be 0.
- Returns:
average, – Return the average along the specified axis.
- 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 ofa
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=np.arange(55)) >>> m.data array([ 1.789, 1.789, ..., 1.222, 1.22])