numpy 1.22.4 Pypi GitHub Homepage
Other Docs
ParametersRaisesReturnsBackRef
average(a, axis=None, weights=None, returned=False)

Parameters

a : array_like

Array containing data to be averaged. If a is not an array, a conversion is attempted.

axis : None 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.

versionadded

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, optional

An array of weights associated with the values in a. Each value in a 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 of a along the given axis) or of the same shape as a. If :None:None:`weights=None`, then all data in a 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 that :None:None:`sum(weights)` must not be 0.

returned : bool, optional

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

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.

Returns

retval, [sum_of_weights] : array_type or double

Return the average along the specified axis. When :None:None:`returned` is :None:None:`True`, return a tuple with the average as the first element and the sum of the weights as the second element. :None:None:`sum_of_weights` is of the same type as retval . The result dtype follows a genereal pattern. If weights is None, the result dtype will be that of a , or float64 if a is integral. Otherwise, if weights is not None and a is non- integral, the result type will be the type of lowest precision capable of representing values of both a and weights . If a happens to be integral, the previous rules still applies but the result dtype will at least be float64 .

Compute the weighted average along the specified axis.

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.

Examples

This example is valid syntax, but we were not able to check execution
>>> data = np.arange(1, 5)
... data array([1, 2, 3, 4])
This example is valid syntax, but we were not able to check execution
>>> np.average(data)
2.5
This example is valid syntax, but we were not able to check execution
>>> np.average(np.arange(1, 11), weights=np.arange(10, 0, -1))
4.0
This example is valid syntax, but we were not able to check execution
>>> data = np.arange(6).reshape((3,2))
... data array([[0, 1], [2, 3], [4, 5]])
This example is valid syntax, but we were not able to check execution
>>> np.average(data, axis=1, weights=[1./4, 3./4])
array([0.75, 2.75, 4.75])
This example is valid syntax, but we were not able to check execution
>>> 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.
This example is valid syntax, but we were not able to check execution
>>> a = np.ones(5, dtype=np.float128)
... w = np.ones(5, dtype=np.complex64)
... avg = np.average(a, weights=w)
... print(avg.dtype) complex256
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.average dask.array.routines.average numpy.mean numpy.sum numpy.nanmean

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/lib/function_base.py#395
type: <class 'function'>
Commit: