bundles / numpy latest / numpy / average
_ArrayFunctionDispatcher
numpy:average
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_function_base_impl.py :439
Signature
def average ( a , axis = None , weights = None , returned = False , * , keepdims = <no value> ) Summary
Compute the weighted average along the specified axis.
Parameters
a: array_likeArray containing data to be averaged. If
ais not an array, a conversion is attempted.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 negative it counts from the last to the first axis. 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, optionalDefault is
False. IfTrue, the tuple (average,sum_of_weights) is returned, otherwise only the average is returned. Ifweights=None,sum_of_weightsis equivalent to the number of elements over which the average is taken.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
retval, [sum_of_weights]: array_type or doubleReturn the average along the specified axis. When
returnedisTrue, return a tuple with the average as the first element and the sum of the weights as the second element.sum_of_weightsis of the same type as retval. The result dtype follows a general pattern. Ifweightsis None, the result dtype will be that ofa, orfloat64ifais integral. Otherwise, ifweightsis not None andais non- integral, the result type will be the type of lowest precision capable of representing values of bothaandweights. Ifahappens to be integral, the previous rules still applies but the result dtype will at least befloat64.
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 data = np.arange(1, 5) data✓
np.average(data) np.average(np.arange(1, 11), weights=np.arange(10, 0, -1))✗
data = np.arange(6).reshape((3, 2)) data np.average(data, axis=1, weights=[1./4, 3./4]) np.average(data, weights=[1./4, 3./4])✓
np.average(data, axis=1, keepdims=True)
✓data = np.arange(8).reshape((2, 2, 2))
✓data
✗np.average(data, axis=(0, 1), weights=[[1./4, 3./4], [1., 1./2]])
✓np.average(data, axis=0, weights=[[1./4, 3./4], [1., 1./2]])
✗See also
- ma.average
average for masked arrays -- useful if your data contains "missing" values
- mean
- numpy.result_type
Returns the type that results from applying the numpy type promotion rules to the arguments.
Aliases
-
numpy.average