MCPcopy Index your code
hub / github.com/numpy/numpy / average

Function average

numpy/lib/_function_base_impl.py:451–611  ·  view source on GitHub ↗

Compute the weighted average along the specified axis. 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

(a, axis=None, weights=None, returned=False, *,
            keepdims=np._NoValue)

Source from the content-addressed store, hash-verified

449
450@array_function_dispatch(_average_dispatcher)
451def average(a, axis=None, weights=None, returned=False, *,
452 keepdims=np._NoValue):
453 """
454 Compute the weighted average along the specified axis.
455
456 Parameters
457 ----------
458 a : array_like
459 Array containing data to be averaged. If `a` is not an array, a
460 conversion is attempted.
461 axis : None or int or tuple of ints, optional
462 Axis or axes along which to average `a`. The default,
463 `axis=None`, will average over all of the elements of the input array.
464 If axis is negative it counts from the last to the first axis.
465 If axis is a tuple of ints, averaging is performed on all of the axes
466 specified in the tuple instead of a single axis or all the axes as
467 before.
468 weights : array_like, optional
469 An array of weights associated with the values in `a`. Each value in
470 `a` contributes to the average according to its associated weight.
471 The array of weights must be the same shape as `a` if no axis is
472 specified, otherwise the weights must have dimensions and shape
473 consistent with `a` along the specified axis.
474 If `weights=None`, then all data in `a` are assumed to have a
475 weight equal to one.
476 The calculation is::
477
478 avg = sum(a * weights) / sum(weights)
479
480 where the sum is over all included elements.
481 The only constraint on the values of `weights` is that `sum(weights)`
482 must not be 0.
483 returned : bool, optional
484 Default is `False`. If `True`, the tuple (`average`, `sum_of_weights`)
485 is returned, otherwise only the average is returned.
486 If `weights=None`, `sum_of_weights` is equivalent to the number of
487 elements over which the average is taken.
488 keepdims : bool, optional
489 If this is set to True, the axes which are reduced are left
490 in the result as dimensions with size one. With this option,
491 the result will broadcast correctly against the original `a`.
492 *Note:* `keepdims` will not work with instances of `numpy.matrix`
493 or other classes whose methods do not support `keepdims`.
494
495 .. versionadded:: 1.23.0
496
497 Returns
498 -------
499 retval, [sum_of_weights] : array_type or double
500 Return the average along the specified axis. When `returned` is `True`,
501 return a tuple with the average as the first element and the sum
502 of the weights as the second element. `sum_of_weights` is of the
503 same type as `retval`. The result dtype follows a general pattern.
504 If `weights` is None, the result dtype will be that of `a` , or ``float64``
505 if `a` is integral. Otherwise, if `weights` is not None and `a` is non-
506 integral, the result type will be the type of lowest precision capable of
507 representing values of both `a` and `weights`. If `a` happens to be
508 integral, the previous rules still applies but the result dtype will

Callers 7

test_basicMethod · 0.90
test_weightsMethod · 0.90
test_returnedMethod · 0.90
test_object_dtypeMethod · 0.90
covFunction · 0.70

Calls 5

_weights_are_validFunction · 0.85
meanMethod · 0.45
sumMethod · 0.45
anyMethod · 0.45
copyMethod · 0.45

Tested by 6

test_basicMethod · 0.72
test_weightsMethod · 0.72
test_returnedMethod · 0.72
test_object_dtypeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…