mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-44150: Support optional weights parameter for fmean() (GH-26175)
This commit is contained in:
parent
18f41c04ff
commit
be4dd7fcd9
4 changed files with 59 additions and 9 deletions
|
@ -136,7 +136,7 @@ from decimal import Decimal
|
|||
from itertools import groupby, repeat
|
||||
from bisect import bisect_left, bisect_right
|
||||
from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum
|
||||
from operator import itemgetter
|
||||
from operator import itemgetter, mul
|
||||
from collections import Counter, namedtuple
|
||||
|
||||
# === Exceptions ===
|
||||
|
@ -345,7 +345,7 @@ def mean(data):
|
|||
return _convert(total / n, T)
|
||||
|
||||
|
||||
def fmean(data):
|
||||
def fmean(data, weights=None):
|
||||
"""Convert data to floats and compute the arithmetic mean.
|
||||
|
||||
This runs faster than the mean() function and it always returns a float.
|
||||
|
@ -363,13 +363,24 @@ def fmean(data):
|
|||
nonlocal n
|
||||
for n, x in enumerate(iterable, start=1):
|
||||
yield x
|
||||
total = fsum(count(data))
|
||||
else:
|
||||
data = count(data)
|
||||
if weights is None:
|
||||
total = fsum(data)
|
||||
try:
|
||||
if not n:
|
||||
raise StatisticsError('fmean requires at least one data point')
|
||||
return total / n
|
||||
except ZeroDivisionError:
|
||||
raise StatisticsError('fmean requires at least one data point') from None
|
||||
try:
|
||||
num_weights = len(weights)
|
||||
except TypeError:
|
||||
weights = list(weights)
|
||||
num_weights = len(weights)
|
||||
num = fsum(map(mul, data, weights))
|
||||
if n != num_weights:
|
||||
raise StatisticsError('data and weights must be the same length')
|
||||
den = fsum(weights)
|
||||
if not den:
|
||||
raise StatisticsError('sum of weights must be non-zero')
|
||||
return num / den
|
||||
|
||||
|
||||
def geometric_mean(data):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue