nums.numpy.average

nums.numpy.average(a, axis=None, weights=None, returned=False)[source]

Compute the weighted average along the specified axis.

This docstring was copied from numpy.average.

Some inconsistencies with the NumS version may exist.

Compute the weighted average along the specified axis.

Parameters
  • a (BlockArray) – 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. 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 (BlockArray, 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 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 sum(weights) must not be 0.

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

Returns

retval, [sum_of_weights] – 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. 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 float.

Return type

array_type or double

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

mean

Notes

Only single ‘axis’ is currently supported.

1D weights broadcasting is currently not supported.

Weights along one or more axes sum to zero.

Examples

The doctests shown below are copied from NumPy. They won’t show the correct result until you operate get().

>>> data = nps.arange(1, 5)  
>>> data.get()  
array([1, 2, 3, 4])
>>> nps.average(data).get()  
array(2.5)
>>> data = nps.arange(6).reshape((3,2))  
>>> data.get()  
array([[0, 1],
       [2, 3],
       [4, 5]])