- ma.average(a, axis=None, weights=None, returned=False, *, keepdims=<no value>)#
Return the weighted average of array over the given axis.
Data to be averaged. Masked entries are not taken into account in the computation.
- axisint, optional
Axis along which to average a. If None, averaging is done over the flattened array.
- weightsarray_like, optional
The importance that each element has in the computation of the average. 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.
- returnedbool, optional
Flag indicating whether a tuple
(result, sum of weights)should be returned as output (True), or just the result (False). Default is False.
- 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: keepdims will not work with instances of
numpy.matrixor other classes whose methods do not support keepdims.
New in version 1.23.0.
- average, [sum_of_weights](tuple of) scalar or MaskedArray
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. The return type is np.float64 if a is of integer type and floats smaller than
float64, or the input data-type, otherwise. If returned, sum_of_weights is always
>>> a = np.ma.array([1., 2., 3., 4.], mask=[False, False, True, True]) >>> np.ma.average(a, weights=[3, 1, 0, 0]) 1.25
>>> x = np.ma.arange(6.).reshape(3, 2) >>> x masked_array( data=[[0., 1.], [2., 3.], [4., 5.]], mask=False, fill_value=1e+20) >>> avg, sumweights = np.ma.average(x, axis=0, weights=[1, 2, 3], ... returned=True) >>> avg masked_array(data=[2.6666666666666665, 3.6666666666666665], mask=[False, False], fill_value=1e+20)
keepdims=True, the following result has shape (3, 1).
>>> np.ma.average(x, axis=1, keepdims=True) masked_array( data=[[0.5], [2.5], [4.5]], mask=False, fill_value=1e+20)