Weighted And Harmonic Means In statistics

fmean applies weights, harmonic_mean averages equal-distance speeds honestly, and geometric_mean fits growth factors like 2x then 8x.

PythonData Integrity

Code

pythonsnippet
17 lines
from statistics import fmean, geometric_mean, harmonic_mean

# Weighted average: projects count more than maintenance work
scores = [3.2, 4.5, 4.0]
weights = [1, 3, 2]
print(f"weighted = {fmean(scores, weights=weights):.3f}")

# Unweighted average of the same scores for contrast
print(f"plain fmean = {fmean(scores):.3f}")

# Harmonic mean is the honest average speed for equal-distance legs
legs_kmh = [60, 120]
print(f"harmonic = {harmonic_mean(legs_kmh)}")

# Geometric mean fits growth factors: 2x then 8x is 4x per step
growth = [2.0, 8.0]
print(f"geometric = {geometric_mean(growth)}")

Output

Captured from a sandboxed run of this snippet in the build pipeline.

weighted = 4.117
plain fmean = 3.900
harmonic = 80.0
geometric = 4.0

The logic behind the lines

Top-level bindings scores, weights, legs_kmh, growth hold the demo state — nothing sits inside a function scope.

Watch out for

  • Nothing third-party is pulled in; the snippet is dependency-free.

Applying it

Output lines are prefixed (weighted =, plain fmean =, harmonic =), so the printed order mirrors the order of the steps.

Interactive Tools for This Topic

FAQ

Are there any imports to resolve outside the language?
No. Every import resolves against the language's own modules. Its state lives in module-level bindings: `scores`, `weights`, `legs_kmh`.