On this page
numpy.average
- numpy.average(a, axis=None, weights=None, returned=False, *, keepdims=<no value>)[source]
-
Compute the weighted average along the specified axis.
- Parameters
-
- aarray_like
-
Array containing data to be averaged. If
ais not an array, a conversion is attempted. - axisNone or int or tuple of ints, optional
-
Axis 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.New in version 1.7.0.
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.
- weightsarray_like, optional
-
An array of weights associated with the values in
a. Each value inacontributes 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 ofaalong the given axis) or of the same shape asa. Ifweights=None, then all data inaare assumed to have a weight equal to one. The 1-D calculation is:avg = sum(a * weights) / sum(weights)The only constraint on
weightsis thatsum(weights)must not be 0. - returnedbool, optional
-
Default 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. - keepdimsbool, optional
-
If 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 ofnumpy.matrixor other classes whose methods do not supportkeepdims.New in version 1.23.0.
- Returns
-
- retval, [sum_of_weights]array_type or double
-
Return 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 asretval. The result dtype follows a genereal 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
-
- ZeroDivisionError
-
When all weights along axis are zero. See
numpy.ma.averagefor a version robust to this type of error. - TypeError
-
When the length of 1D
weightsis not the same as the shape ofaalong axis.
See also
-
mean -
ma.average -
average for masked arrays – useful if your data contains “missing” values
-
numpy.result_type -
Returns the type that results from applying the numpy type promotion rules to the arguments.
Examples
>>> data = np.arange(1, 5) >>> data array([1, 2, 3, 4]) >>> np.average(data) 2.5 >>> np.average(np.arange(1, 11), weights=np.arange(10, 0, -1)) 4.0>>> data = np.arange(6).reshape((3, 2)) >>> data array([[0, 1], [2, 3], [4, 5]]) >>> np.average(data, axis=1, weights=[1./4, 3./4]) array([0.75, 2.75, 4.75]) >>> np.average(data, weights=[1./4, 3./4]) Traceback (most recent call last): ... TypeError: Axis must be specified when shapes of a and weights differ.>>> a = np.ones(5, dtype=np.float128) >>> w = np.ones(5, dtype=np.complex64) >>> avg = np.average(a, weights=w) >>> print(avg.dtype) complex256With
keepdims=True, the following result has shape (3, 1).>>> np.average(data, axis=1, keepdims=True) array([[0.5], [2.5], [4.5]])
© 2005–2022 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/1.23/reference/generated/numpy.average.html