dpnp.average
- dpnp.average(a, axis=None, weights=None, returned=False, *, keepdims=False)[source]
Compute the weighted average along the specified axis.
For full documentation refer to
numpy.average
.- Parameters:
a ({dpnp.ndarray, usm_ndarray}) -- Input array.
axis ({None, int, tuple of ints}, optional) -- Axis or axes along which the averages must be computed. If a tuple of unique integers, the averages are computed over multiple axes. If
None
, the average is computed over the entire array. Default:None
.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 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) -- 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. Default:False
.keepdims ({None, bool}, optional) -- If
True
, the reduced axes (dimensions) are included in the result as singleton dimensions, so that the returned array remains compatible with the input array according to Array Broadcasting rules. Otherwise, ifFalse
, the reduced axes are not included in the returned array. Default:False
.
- Returns:
out, [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 out. The result dtype follows a general pattern. If weights isNone
, the result dtype will be that of a , or default floating point data type for the device where input array a is allocated. Otherwise, if weights is notNone
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 default floating point data type for the device where input array a is allocated.- Return type:
dpnp.ndarray, dpnp.ndarray
See also
Examples
>>> import dpnp as np >>> data = np.arange(1, 5) >>> data array([1, 2, 3, 4]) >>> np.average(data) array(2.5) >>> np.average(np.arange(1, 11), weights=np.arange(10, 0, -1)) array(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]) TypeError: Axis must be specified when shapes of a and weights differ.
With
keepdims=True
, the following result has shape (3, 1).>>> np.average(data, axis=1, keepdims=True) array([[0.5], [2.5], [4.5]])
>>> a = np.ones(5, dtype=np.float64) >>> w = np.ones(5, dtype=np.complex64) >>> avg = np.average(a, weights=w) >>> print(avg.dtype) complex128