def average(a, axis=None, weights=None, returned=False):
a = asarray(a)<EOL>mask = a.mask<EOL>ash = a.shape<EOL>if ash == ():<EOL><INDENT>ash = (<NUM_LIT:1>,)<EOL><DEDENT>if axis is None:<EOL><INDENT>if mask is nomask:<EOL><INDENT>if weights is None:<EOL><INDENT>n = a.sum(axis=None)<EOL>d = float(a.size)<EOL><DEDENT>else:<EOL><INDENT>w = filled(weights, <NUM_LIT:0.0>).ravel()<EOL>n = umath.add.reduce(a._data.ravel() * w)<EOL>d = umath.add.reduce(w)<EOL>del w<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if weights is None:<EOL><INDENT>n = a.filled(<NUM_LIT:0>).sum(axis=None)<EOL>d = float(umath.add.reduce((~mask).ravel()))<EOL><DEDENT>else:<EOL><INDENT>w = array(filled(weights, <NUM_LIT:0.0>), float, mask=mask).ravel()<EOL>n = add.reduce(a.ravel() * w)<EOL>d = add.reduce(w)<EOL>del w<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if mask is nomask:<EOL><INDENT>if weights is None:<EOL><INDENT>d = ash[axis] * <NUM_LIT:1.0><EOL>n = add.reduce(a._data, axis)<EOL><DEDENT>else:<EOL><INDENT>w = filled(weights, <NUM_LIT:0.0>)<EOL>wsh = w.shape<EOL>if wsh == ():<EOL><INDENT>wsh = (<NUM_LIT:1>,)<EOL><DEDENT>if wsh == ash:<EOL><INDENT>w = np.array(w, float, copy=<NUM_LIT:0>)<EOL>n = add.reduce(a * w, axis)<EOL>d = add.reduce(w, axis)<EOL>del w<EOL><DEDENT>elif wsh == (ash[axis],):<EOL><INDENT>ni = ash[axis]<EOL>r = [None] * len(ash)<EOL>r[axis] = slice(None, None, <NUM_LIT:1>)<EOL>w = eval ("<STR_LIT>" + repr(tuple(r)) + "<STR_LIT>")<EOL>n = add.reduce(a * w, axis)<EOL>d = add.reduce(w, axis, dtype=float)<EOL>del w, r<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if weights is None:<EOL><INDENT>n = add.reduce(a, axis)<EOL>d = umath.add.reduce((~mask), axis=axis, dtype=float)<EOL><DEDENT>else:<EOL><INDENT>w = filled(weights, <NUM_LIT:0.0>)<EOL>wsh = w.shape<EOL>if wsh == ():<EOL><INDENT>wsh = (<NUM_LIT:1>,)<EOL><DEDENT>if wsh == ash:<EOL><INDENT>w = array(w, dtype=float, mask=mask, copy=<NUM_LIT:0>)<EOL>n = add.reduce(a * w, axis)<EOL>d = add.reduce(w, axis, dtype=float)<EOL><DEDENT>elif wsh == (ash[axis],):<EOL><INDENT>ni = ash[axis]<EOL>r = [None] * len(ash)<EOL>r[axis] = slice(None, None, <NUM_LIT:1>)<EOL>w = eval ("<STR_LIT>" + repr(tuple(r)) +"<STR_LIT>")<EOL>n = add.reduce(a * w, axis)<EOL>d = add.reduce(w, axis, dtype=float)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>del w<EOL><DEDENT><DEDENT><DEDENT>if n is masked or d is masked:<EOL><INDENT>return masked<EOL><DEDENT>result = n / d<EOL>del n<EOL>if isinstance(result, MaskedArray):<EOL><INDENT>if ((axis is None) or (axis == <NUM_LIT:0> and a.ndim == <NUM_LIT:1>)) and(result.mask is nomask):<EOL><INDENT>result = result._data<EOL><DEDENT>if returned:<EOL><INDENT>if not isinstance(d, MaskedArray):<EOL><INDENT>d = masked_array(d)<EOL><DEDENT>if isinstance(d, ndarray) and (not d.shape == result.shape):<EOL><INDENT>d = ones(result.shape, dtype=float) * d<EOL><DEDENT><DEDENT><DEDENT>if returned:<EOL><INDENT>return result, d<EOL><DEDENT>else:<EOL><INDENT>return result<EOL><DEDENT>
Return the weighted average of array over the given axis.

Parameters
----------
a : array_like
    Data to be averaged.
    Masked entries are not taken into account in the computation.
axis : int, optional
    Axis along which the average is computed. The default is to compute
    the average of the flattened array.
weights : array_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.   If `weights` is complex, the imaginary parts
    are ignored.
returned : bool, optional
    Flag indicating whether a tuple ``(result, sum of weights)``
    should be returned as output (True), or just the result (False).
    Default is False.

Returns
-------
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, otherwise it is of the same type as `a`.
    If returned, `sum_of_weights` is of the same type as `average`.

Examples
--------
>>> 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)
>>> print x
[[ 0.  1.]
 [ 2.  3.]
 [ 4.  5.]]
>>> avg, sumweights = np.ma.average(x, axis=0, weights=[1, 2, 3],
...                                 returned=True)
>>> print avg
[2.66666666667 3.66666666667]