bundles / numpy latest / numpy / ma / extras / average
function
numpy.ma.extras:average
source: build-install/usr/lib/python3.14/site-packages/numpy/ma/extras.py :510
Signature
def average ( a , axis = None , weights = None , returned = False , * , keepdims = <no value> ) Summary
Return the weighted average of array over the given axis.
Parameters
a: array_likeData to be averaged. Masked entries are not taken into account in the computation.
axis: None or int or tuple of ints, optionalAxis or axes along which to average
a. The default,axis=None, will average over all of the elements of the input array. If axis is a tuple of ints, averaging is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.weights: array_like, optionalAn array of weights associated with the values in
a. Each value inacontributes to the average according to its associated weight. The array of weights must be the same shape asaif no axis is specified, otherwise the weights must have dimensions and shape consistent withaalong the specified axis. Ifweights=None, then all data inaare assumed to have a weight equal to one. The calculation isavg = sum(a * weights) / sum(weights)where the sum is over all included elements. The only constraint on the values of
weightsis thatsum(weights)must not be 0.returned: bool, optionalFlag indicating whether a tuple
(result, sum of weights)should be returned as output (True), or just the result (False). Default is False.keepdims: bool, optionalIf 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 original
a. Note:keepdimswill not work with instances of numpy.matrix or other classes whose methods do not supportkeepdims.
Returns
average, [sum_of_weights]: (tuple of) scalar or MaskedArrayThe average along the specified axis. When returned is
True, return a tuple with the average as the first element and the sum of the weights as the second element. The return type isnp.float64ifais of integer type and floats smaller than float64, or the input data-type, otherwise. If returned,sum_of_weightsis always float64.
Raises
: ZeroDivisionErrorWhen all weights along axis are zero. See numpy.ma.average for a version robust to this type of error.
: TypeErrorWhen
weightsdoes not have the same shape asa, andaxis=None.: ValueErrorWhen
weightsdoes not have dimensions and shape consistent withaalong specifiedaxis.
Examples
import numpy as np a = np.ma.array([1., 2., 3., 4.], mask=[False, False, True, True])✓
np.ma.average(a, weights=[3, 1, 0, 0])
✗x = np.ma.arange(6.).reshape(3, 2) x data = np.arange(8).reshape((2, 2, 2))✓
data np.ma.average(data, axis=(0, 1), weights=[[1./4, 3./4], [1., 1./2]]) np.ma.average(data, axis=0, weights=[[1./4, 3./4], [1., 1./2]])✗
avg, sumweights = np.ma.average(x, axis=0, weights=[1, 2, 3], returned=True) avg✓
np.ma.average(x, axis=1, keepdims=True)
✓Aliases
-
numpy.ma.average