Reference · Statistics
stats.mean
stats.mean(list) -> floatWhat it does
The arithmetic mean of a list of numbers, as a float: the sum divided by the count.
It is Python's statistics.mean: the sum is compensated and rounded once, so the
answer is the correctly rounded mean, not the running sum divided by the count.
Ints and floats can mix; the answer is always a float.
Examples
print(stats.mean([1, 2, 3, 4]), stats.mean([1, 2, 3]), stats.mean([1.5, 2.5]))print(stats.mean([0.1, 0.2, 0.3]), (0.1 + 0.2 + 0.3) / 3)2.5 2.0 2.00.2 0.20000000000000004Edge cases
- One number gives that number, as a float.
- The list is read into floats first: an int past 2⁵³ (about 9 × 10¹⁵) loses its last digits, where Python keeps them exact.
- Floats whose sum overflows the largest float still give their mean, as in Python.
- Decimals are not accepted; convert them with
float.
print(stats.mean([5]), stats.mean([-1, 1]))print(stats.mean([9007199254740993, 9007199254740993]))print(stats.mean([1e308, 1e308]))print(stats.mean([dec("1.5"), dec("2.5")] | map(float)))5.0 0.09007199254740992.01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002.0Errors
The mean of no data is a runtime error, not an error value: an empty list or null
stops the request. So does a list with something that is not a number in it, or a
decimal. Guard with len when the list can be empty.
print(stats.mean([]))line 1: `stats.mean` requires at least one data pointprint(stats.mean([1, "2"]))line 1: `stats.mean` expects a list of numbers, but item 1 is strlet xs = []print(len(xs) > 0 && stats.mean(xs))falseChaining
In a pipeline the list comes first, so stats.mean takes it as its only argument:
print([3, 1, 2] | stats.mean)print([{ total: 12.5 }, { total: 30 }, { total: 7.5 }] | vec.column("total") | stats.mean)print([1, 2, 3, 10] | filter((x) => x < 10) | stats.mean)2.016.6666666666666682.0See also
stats.fmean, stats.median, stats.geometric_mean, stats.harmonic_mean, stats.describe, sum, math.fsum
Thread 0 posts
On stats.mean(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo