summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2019-08-27 11:19:34 +0300
committerGitHub <noreply@github.com>2019-08-27 11:19:34 +0300
commit17ff82a7f3ca49717128b89d1a5560f4545fda0f (patch)
tree1605404fee965445a644ae1ceeebbe0fa055ac0a /numpy
parent123db90bfc977165577347beaaa2849a6747593b (diff)
parentad49532a21cc007d4a1f0ba42f2a25db013c6505 (diff)
downloadnumpy-17ff82a7f3ca49717128b89d1a5560f4545fda0f.tar.gz
Merge pull request #14365 from WarrenWeckesser/doc-average
DOC: lib: Add more explanation of the weighted average calculation.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/function_base.py7
-rw-r--r--numpy/ma/extras.py7
2 files changed, 10 insertions, 4 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 9d380e67d..21532838b 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -316,14 +316,17 @@ def average(a, axis=None, weights=None, returned=False):
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.
+ weight equal to one. The 1-D calculation is::
+
+ avg = sum(a * weights) / sum(weights)
+
+ The only constraint on `weights` is that `sum(weights)` must not be 0.
returned : bool, optional
Default is `False`. If `True`, the tuple (`average`, `sum_of_weights`)
is returned, otherwise only the average is returned.
If `weights=None`, `sum_of_weights` is equivalent to the number of
elements over which the average is taken.
-
Returns
-------
retval, [sum_of_weights] : array_type or double
diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py
index 639b3dd1f..de1aa3af8 100644
--- a/numpy/ma/extras.py
+++ b/numpy/ma/extras.py
@@ -549,8 +549,11 @@ def average(a, axis=None, weights=None, returned=False):
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.
+ weight equal to one. The 1-D calculation is::
+
+ avg = sum(a * weights) / sum(weights)
+
+ The only constraint on `weights` is that `sum(weights)` must not be 0.
returned : bool, optional
Flag indicating whether a tuple ``(result, sum of weights)``
should be returned as output (True), or just the result (False).