code: def average(
      a, axis=None, weights=None, returned=False, *, keepdims=False
  ):
      """Returns the weighted average along an axis.
  
      Args:
          a (cupy.ndarray): Array to compute average.
          axis (int): Along which axis to compute average. The flattened array
              is used by default.
          weights (cupy.ndarray): Array of weights where each element
              corresponds to the value in ``a``. If ``None``, all the values
              in ``a`` have a weight equal to one.
          returned (bool): If ``True``, a tuple of the average and the sum
              of weights is returned, otherwise only the average is returned.
          keepdims (bool): If ``True``, the axis is remained as an axis of size
              one.
  
      Returns:
          cupy.ndarray or tuple of cupy.ndarray: The average of the input array
          along the axis and the sum of weights.
  
      .. warning::
  
          This function may synchronize the device if ``weight`` is given.
  
      .. seealso:: :func:`numpy.average`
      """
      # TODO(niboshi): Avoid synchronization.
      a = cupy.asarray(a)
  
      if weights is None:
          avg = a.mean(axis=axis, keepdims=keepdims)
          scl = avg.dtype.type(a.size / avg.size)
      else:
          wgt = cupy.asarray(weights)
  
          if issubclass(a.dtype.type, (numpy.integer, numpy.bool_)):
              result_dtype = functools.reduce(
                  numpy.promote_types, (a.dtype, wgt.dtype, "f8")
              )
          else:
              result_dtype = numpy.promote_types(a.dtype, wgt.dtype)
  
          # Sanity checks
          if a.shape != wgt.shape:
              if axis is None:
                  raise TypeError(
                      "Axis must be specified when shapes of a and weights "
                      "differ."
                  )
              if wgt.ndim != 1:
                  raise TypeError(
                      "1D weights expected when shapes of a and weights differ."
                  )
              if wgt.shape[0] != a.shape[axis]:
                  raise ValueError(
                      "Length of weights not compatible with specified axis."
                  )
  
              # setup wgt to broadcast along axis
              wgt = cupy.broadcast_to(
                  wgt, (a.ndim - 1) * (1,) + wgt.shape
              )
              wgt = wgt.swapaxes(-1, axis)
  
          scl = wgt.sum(
              axis=axis, dtype=result_dtype, keepdims=keepdims
          )
          if cupy.any(scl == 0.0):  # synchronize!
              raise ZeroDivisionError(
                  "Weights sum to zero, can't be normalized"
              )
  
          avg = (
              cupy.multiply(a, wgt, dtype=result_dtype).sum(
                  axis, keepdims=keepdims
              )
              / scl
          )
  
      if returned:
          if scl.shape != avg.shape:
              scl = cupy.broadcast_to(cupy.array(scl), avg.shape).copy()
          return avg, scl
      else:
          return avg
  
file_path: cupy/cupy/_statistics/meanvar.py
imports: import functools
  import numpy
  import cupy
  from cupy._core import _routines_statistics
label_desc: Use image node_id 3 for calling a global function with example usage: average(a, axis, weights, returned) and returns: avg,  scl, avg