|
136 | 136 | from decimal import Decimal |
137 | 137 | from itertools import count, groupby, repeat |
138 | 138 | from bisect import bisect_left, bisect_right |
139 | | -from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum |
| 139 | +from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum, sumprod |
140 | 140 | from functools import reduce |
141 | | -from operator import mul, itemgetter |
| 141 | +from operator import itemgetter |
142 | 142 | from collections import Counter, namedtuple, defaultdict |
143 | 143 |
|
144 | 144 | _SQRT2 = sqrt(2.0) |
@@ -496,28 +496,26 @@ def fmean(data, weights=None): |
496 | 496 | >>> fmean([3.5, 4.0, 5.25]) |
497 | 497 | 4.25 |
498 | 498 | """ |
499 | | - try: |
500 | | - n = len(data) |
501 | | - except TypeError: |
502 | | - # Handle iterators that do not define __len__(). |
503 | | - n = 0 |
504 | | - def count(iterable): |
505 | | - nonlocal n |
506 | | - for n, x in enumerate(iterable, start=1): |
507 | | - yield x |
508 | | - data = count(data) |
509 | 499 | if weights is None: |
| 500 | + try: |
| 501 | + n = len(data) |
| 502 | + except TypeError: |
| 503 | + # Handle iterators that do not define __len__(). |
| 504 | + n = 0 |
| 505 | + def count(iterable): |
| 506 | + nonlocal n |
| 507 | + for n, x in enumerate(iterable, start=1): |
| 508 | + yield x |
| 509 | + data = count(data) |
510 | 510 | total = fsum(data) |
511 | 511 | if not n: |
512 | 512 | raise StatisticsError('fmean requires at least one data point') |
513 | 513 | return total / n |
514 | | - try: |
515 | | - num_weights = len(weights) |
516 | | - except TypeError: |
| 514 | + if not isinstance(weights, (list, tuple)): |
517 | 515 | weights = list(weights) |
518 | | - num_weights = len(weights) |
519 | | - num = fsum(map(mul, data, weights)) |
520 | | - if n != num_weights: |
| 516 | + try: |
| 517 | + num = sumprod(data, weights) |
| 518 | + except ValueError: |
521 | 519 | raise StatisticsError('data and weights must be the same length') |
522 | 520 | den = fsum(weights) |
523 | 521 | if not den: |
|
0 commit comments