# dask.array.average

dask.array.average(a, axis=None, weights=None, returned=False, keepdims=False)

  Compute the weighted average along the specified axis.

  This docstring was copied from numpy.average.

  Some inconsistencies with the Dask version may exist.

  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. 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 array of weights must be the same shape as a if no axis is specified, otherwise the weights must have dimensions and shape consistent with a along the specified axis. If weights=None, then all data in a are assumed to have a weight equal to one. The calculation is:

          avg = sum(a * weights) / sum(weights)

      where the sum is over all included elements. The only constraint on the values of 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.

    keepdims bool, 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.matrix or other classes whose methods do not support keepdims.

      Added in version 1.23.0.

  Returns:
    retval, [sum_of_weights]array_type or double

      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 general 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`.

  Raises:
    ZeroDivisionError

      When all weights along axis are zero. See numpy.ma.average for a version robust to this type of error.

    TypeError

      When weights does not have the same shape as a, and axis=None.

    ValueError

      When weights does not have dimensions and shape consistent with a along specified axis.

  See also

  `mean`
  `ma.average`

    average for masked arrays – useful if your data contains “missing” values

  `numpy.result_type`

    Returns the type that results from applying the numpy type promotion rules to the arguments.

  Examples

      >>> import numpy as np
      >>> data = np.arange(1, 5)
      >>> data
      array([1, 2, 3, 4])
      >>> np.average(data)
      2.5
      >>> np.average(np.arange(1, 11), weights=np.arange(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.

  With `keepdims=True`, the following result has shape (3, 1).

      >>> np.average(data, axis=1, keepdims=True)
      array([[0.5],
             [2.5],
             [4.5]])

      >>> data = np.arange(8).reshape((2, 2, 2))
      >>> data
      array([[[0, 1],
              [2, 3]],
             [[4, 5],
              [6, 7]]])
      >>> np.average(data, axis=(0, 1), weights=[[1./4, 3./4], [1., 1./2]])
      array([3.4, 4.4])
      >>> np.average(data, axis=0, weights=[[1./4, 3./4], [1., 1./2]])
      Traceback (most recent call last):
          ...
      ValueError: Shape of weights must be consistent
      with shape of a along specified axis.