Reference · Statistics
stats.fmean
stats.fmean(list, list | null=) -> floatWhat it does
The mean of a list of numbers as a float, optionally weighted. With a second list of
weights, one per value, the answer is the sum of each value times its weight, divided
by the sum of the weights. Without weights, or with null, it is the same as
stats.mean: Python's fmean is only a faster
mean, and here both give the same float.
Examples
print(stats.fmean([1, 2, 3, 4]))print(stats.fmean([80, 90], [0.6, 0.4]))print(stats.fmean([1, 2, 3], [3, 1, 1]))2.584.01.6Edge cases
- Weights can be ints or floats and need not add up to 1.
- A weight can be negative or zero as long as the weights' sum is not zero.
nullfor the weights means no weights.- One value gives that value, as a float.
print(stats.fmean([1, 2, 3], [1, 1, 1]), stats.fmean([1, 2, 3], null))print(stats.fmean([1, 2], [2, -1]), stats.fmean([5], [10]))2.0 2.00.0 5.0Errors
No data, weights of another length than the data, and weights that add up to zero are each a runtime error, not an error value. So is anything in either list that is not a number.
print(stats.fmean([1, 2], [1]))line 1: `stats.fmean`: data and weights must be the same lengthprint(stats.fmean([1, 2], [0, 0]))line 1: `stats.fmean`: the sum of the weights must be non-zeroprint(stats.fmean([]))line 1: `stats.fmean` requires at least one data pointChaining
In a pipeline the data comes first and the weights are the argument:
let grades = [80, 90, 70]let credits = [3, 1, 2]print(grades | stats.fmean(credits))print(grades | stats.fmean | round(1))78.3333333333333380.0See also
stats.mean, stats.geometric_mean, stats.harmonic_mean, sum
Thread 0 posts
On stats.fmean(): 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