[issue35904] Add statistics.fmean(seq)
Raymond Hettinger
report at bugs.python.org
Thu Feb 7 19:10:58 EST 2019
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
>> def fmean(seq: Sequence[float]) -> float:
>> return math.fsum(seq) / len(seq)
>
> Is it intentional that this doesn't support iterators?
Since we need both the sum and the length, this seemed like a good starting point. Also, the existing mean() function already covers the more general cases.
I suspect that it is common to keep the data in memory so that more than one descriptive statistic can be generated:
data = load_measurements()
data.sort()
n = len(data)
mu = fastmean(data)
sigma = stdev(data, xbar=mu)
low, q1, q2, q3, high = data[0], data[n//4], data[n//2], data[3*n//4], data[-1]
popular = mode(data, first_tie=True)
It's possible (though possibly not desirable) to provide an fallback path:
def fastmean(data: Iterable) -> float:
try:
return fsum(data) / len(data)
except TypeError:
# Slow alternative
return float(mean(data))
# Memory intensive alternative
data = list(data)
return fsum(data) / len(data)
# Less accurate alternative
total = n = 0
for n, x in enumerate(data, start=1):
total += x
return total / n
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35904>
_______________________________________
More information about the Python-bugs-list
mailing list