Skip to content

Commit

Permalink
Optimize fmean() weighted average (python#102626)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger authored Mar 12, 2023
1 parent e621062 commit 6cd7572
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@
from decimal import Decimal
from itertools import count, groupby, repeat
from bisect import bisect_left, bisect_right
from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum
from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum, sumprod
from functools import reduce
from operator import mul, itemgetter
from operator import itemgetter
from collections import Counter, namedtuple, defaultdict

_SQRT2 = sqrt(2.0)
Expand Down Expand Up @@ -496,28 +496,26 @@ def fmean(data, weights=None):
>>> fmean([3.5, 4.0, 5.25])
4.25
"""
try:
n = len(data)
except TypeError:
# Handle iterators that do not define __len__().
n = 0
def count(iterable):
nonlocal n
for n, x in enumerate(iterable, start=1):
yield x
data = count(data)
if weights is None:
try:
n = len(data)
except TypeError:
# Handle iterators that do not define __len__().
n = 0
def count(iterable):
nonlocal n
for n, x in enumerate(iterable, start=1):
yield x
data = count(data)
total = fsum(data)
if not n:
raise StatisticsError('fmean requires at least one data point')
return total / n
try:
num_weights = len(weights)
except TypeError:
if not isinstance(weights, (list, tuple)):
weights = list(weights)
num_weights = len(weights)
num = fsum(map(mul, data, weights))
if n != num_weights:
try:
num = sumprod(data, weights)
except ValueError:
raise StatisticsError('data and weights must be the same length')
den = fsum(weights)
if not den:
Expand Down

0 comments on commit 6cd7572

Please sign in to comment.