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 : None or int or tuple of ints, optional Axis or axes along which to av
(a, axis=None, weights=None, returned=False, *,
keepdims=np._NoValue)
| 508 | |
| 509 | |
| 510 | def average(a, axis=None, weights=None, returned=False, *, |
| 511 | keepdims=np._NoValue): |
| 512 | """ |
| 513 | Return the weighted average of array over the given axis. |
| 514 | |
| 515 | Parameters |
| 516 | ---------- |
| 517 | a : array_like |
| 518 | Data to be averaged. |
| 519 | Masked entries are not taken into account in the computation. |
| 520 | axis : None or int or tuple of ints, optional |
| 521 | Axis or axes along which to average `a`. The default, |
| 522 | `axis=None`, will average over all of the elements of the input array. |
| 523 | If axis is a tuple of ints, averaging is performed on all of the axes |
| 524 | specified in the tuple instead of a single axis or all the axes as |
| 525 | before. |
| 526 | weights : array_like, optional |
| 527 | An array of weights associated with the values in `a`. Each value in |
| 528 | `a` contributes to the average according to its associated weight. |
| 529 | The array of weights must be the same shape as `a` if no axis is |
| 530 | specified, otherwise the weights must have dimensions and shape |
| 531 | consistent with `a` along the specified axis. |
| 532 | If `weights=None`, then all data in `a` are assumed to have a |
| 533 | weight equal to one. |
| 534 | The calculation is:: |
| 535 | |
| 536 | avg = sum(a * weights) / sum(weights) |
| 537 | |
| 538 | where the sum is over all included elements. |
| 539 | The only constraint on the values of `weights` is that `sum(weights)` |
| 540 | must not be 0. |
| 541 | returned : bool, optional |
| 542 | Flag indicating whether a tuple ``(result, sum of weights)`` |
| 543 | should be returned as output (True), or just the result (False). |
| 544 | Default is False. |
| 545 | keepdims : bool, optional |
| 546 | If this is set to True, the axes which are reduced are left |
| 547 | in the result as dimensions with size one. With this option, |
| 548 | the result will broadcast correctly against the original `a`. |
| 549 | *Note:* `keepdims` will not work with instances of `numpy.matrix` |
| 550 | or other classes whose methods do not support `keepdims`. |
| 551 | |
| 552 | .. versionadded:: 1.23.0 |
| 553 | |
| 554 | Returns |
| 555 | ------- |
| 556 | average, [sum_of_weights] : (tuple of) scalar or MaskedArray |
| 557 | The average along the specified axis. When returned is `True`, |
| 558 | return a tuple with the average as the first element and the sum |
| 559 | of the weights as the second element. The return type is `np.float64` |
| 560 | if `a` is of integer type and floats smaller than `float64`, or the |
| 561 | input data-type, otherwise. If returned, `sum_of_weights` is always |
| 562 | `float64`. |
| 563 | |
| 564 | Raises |
| 565 | ------ |
| 566 | ZeroDivisionError |
| 567 | When all weights along axis are zero. See `numpy.ma.average` for a |
searching dependent graphs…