Reference · Statistics

stats.harmonic_mean

signature
stats.harmonic_mean(list) -> float

What it does

The harmonic mean of a list of numbers that are zero or more: the count divided by the sum of the reciprocals, as Python's statistics.harmonic_mean. It is the mean for rates: a trip at 40 km/h one way and 60 km/h back averages 48 km/h, not 50.

Examples

print(stats.harmonic_mean([40, 60]))print(stats.harmonic_mean([60, 60]))print(stats.harmonic_mean([2.5, 3, 10]))
output
48.060.03.5999999999999996

Edge cases

print(stats.harmonic_mean([1, 0, 3]), stats.harmonic_mean([7]))
output
0.0 7.0

Errors

No data, a negative number, or something that is not a number are each a runtime error, not an error value.

this one fails
print(stats.harmonic_mean([1, -2]))
output
line 1: `stats.harmonic_mean` does not support negative values
this one fails
print(stats.harmonic_mean([]))
output
line 1: `stats.harmonic_mean` requires at least one data point

Chaining

let speeds = [40, 60, 80]print(speeds | stats.harmonic_mean | round(2))
output
55.38

See also

stats.mean, stats.geometric_mean, stats.fmean

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On stats.harmonic_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