numpy.average(a, axis=None, weights=None, returned=False)[source]
Compute the weighted average along the specified axis.
Parameters: |
a : array_like Array containing data to be averaged. If axis : int, optional Axis along which to average weights : array_like, optional An array of weights associated with the values in returned : bool, optional Default is |
---|---|
Returns: |
average, [sum_of_weights] : array_type or double Return the average along the specified axis. When returned is |
Raises: |
ZeroDivisionError When all weights along axis are zero. See TypeError When the length of 1D |
>>> 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
>>> 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. Traceback (most recent call last): ... TypeError: Axis must be specified when shapes of a and weights differ.
© 2008–2016 NumPy Developers
Licensed under the NumPy License.
https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.average.html