numpy.average

numpy.average(a, axis=None, weights=None, returned=False)

Return the weighted average of array over the specified axis.

Parameters:

a : array_like

Data to be averaged.

axis : int, optional

Axis along which to average a. If None, averaging is done over the entire array irrespective of its shape.

weights : array_like, optional

The importance that each datum has in the computation of the average. The weights array can either be 1-D (in which case its length must be the size of a along the given axis) or of the same shape as a. If weights=None, then all data in a are assumed to have a weight equal to one.

returned : bool, optional

Default is False. If True, the tuple (average, sum_of_weights) is returned, otherwise only the average is returned. Note that if weights=None, sum_of_weights is equivalent to the number of elements over which the average is taken.

Returns:

average, [sum_of_weights] : {array_type, double}

Return the 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 is Float if a is of integer type, otherwise it is of the same type as a. sum_of_weights is of the same type as average.

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

ma.average
average for masked arrays

Examples

>>> data = range(1,5)
>>> data
[1, 2, 3, 4]
>>> np.average(data)
2.5
>>> np.average(range(1,11), weights=range(10,0,-1))
4.0

Previous topic

numpy.ptp

Next topic

numpy.mean

This Page

Quick search