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)
| 397 | |
| 398 | @array_function_dispatch(_average_dispatcher) |
| 399 | def average(a, axis=None, weights=None, returned=False, *, |
| 400 | keepdims=np._NoValue): |
| 401 | """ |
| 402 | Compute the weighted average along the specified axis. |
| 403 | |
| 404 | Parameters |
| 405 | ---------- |
| 406 | a : array_like |
| 407 | Array containing data to be averaged. If `a` is not an array, a |
| 408 | conversion is attempted. |
| 409 | axis : None or int or tuple of ints, optional |
| 410 | Axis or axes along which to average `a`. The default, |
| 411 | axis=None, will average over all of the elements of the input array. |
| 412 | If axis is negative it counts from the last to the first axis. |
| 413 | |
| 414 | .. versionadded:: 1.7.0 |
| 415 | |
| 416 | If axis is a tuple of ints, averaging is performed on all of the axes |
| 417 | specified in the tuple instead of a single axis or all the axes as |
| 418 | before. |
| 419 | weights : array_like, optional |
| 420 | An array of weights associated with the values in `a`. Each value in |
| 421 | `a` contributes to the average according to its associated weight. |
| 422 | The weights array can either be 1-D (in which case its length must be |
| 423 | the size of `a` along the given axis) or of the same shape as `a`. |
| 424 | If `weights=None`, then all data in `a` are assumed to have a |
| 425 | weight equal to one. The 1-D calculation is:: |
| 426 | |
| 427 | avg = sum(a * weights) / sum(weights) |
| 428 | |
| 429 | The only constraint on `weights` is that `sum(weights)` must not be 0. |
| 430 | returned : bool, optional |
| 431 | Default is `False`. If `True`, the tuple (`average`, `sum_of_weights`) |
| 432 | is returned, otherwise only the average is returned. |
| 433 | If `weights=None`, `sum_of_weights` is equivalent to the number of |
| 434 | elements over which the average is taken. |
| 435 | keepdims : bool, optional |
| 436 | If this is set to True, the axes which are reduced are left |
| 437 | in the result as dimensions with size one. With this option, |
| 438 | the result will broadcast correctly against the original `a`. |
| 439 | *Note:* `keepdims` will not work with instances of `numpy.matrix` |
| 440 | or other classes whose methods do not support `keepdims`. |
| 441 | |
| 442 | .. versionadded:: 1.23.0 |
| 443 | |
| 444 | Returns |
| 445 | ------- |
| 446 | retval, [sum_of_weights] : array_type or double |
| 447 | Return the average along the specified axis. When `returned` is `True`, |
| 448 | return a tuple with the average as the first element and the sum |
| 449 | of the weights as the second element. `sum_of_weights` is of the |
| 450 | same type as `retval`. The result dtype follows a genereal pattern. |
| 451 | If `weights` is None, the result dtype will be that of `a` , or ``float64`` |
| 452 | if `a` is integral. Otherwise, if `weights` is not None and `a` is non- |
| 453 | integral, the result type will be the type of lowest precision capable of |
| 454 | representing values of both `a` and `weights`. If `a` happens to be |
| 455 | integral, the previous rules still applies but the result dtype will |
| 456 | at least be ``float64``. |