ML Foundations

Python mean preserves Fraction; fmean returns float

Choose statistics.mean when the input numeric type matters. statistics.fmean always returns a float, including for Fraction input.

Example results for Python mean preserves Fraction; fmean returns float. mean: 3/20 Fraction
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

The two arithmetic means have different result contracts

`statistics.mean` and `statistics.fmean` both calculate arithmetic averages, but they do not promise the same result type. `mean` supports exact rational values such as `fractions.Fraction` and returns a result compatible with that input. `fmean` converts values to floating point and always returns a `float`. [1, 2]

That distinction matters when downstream code expects exact rational arithmetic or checks a value type. A decimal-looking result from `fmean` may also carry ordinary binary floating-point representation effects. Choose based on the numeric contract required by later calculations, not merely on which function has the shorter name. [1, 2]

References: [1], [2]

See the type difference with exact fractions

The example averages one tenth and two tenths as `Fraction` values. `mean` returns the exact rational value three twentieths. `fmean` returns a float whose printed representation is `0.15000000000000002`. The comparison uses `math.isclose` for the approximate result, while the exact result is checked by rational equality. [1, 2]

This is not a claim that `mean` is always preferable or that `fmean` is inaccurate for ordinary floating-point work. The functions express different result contracts. The official documentation notes that `fmean` returns a float and is faster in its intended floating-point use, while `mean` supports rational inputs. [1, 2]

Executed with Python 3.12.14, standard library only; assertions passed.

"""Compare exact Fraction output from mean with float output from fmean."""
from fractions import Fraction
from math import isclose
from statistics import fmean, mean

values = (Fraction(1, 10), Fraction(2, 10))
exact_mean = mean(values)
float_mean = fmean(values)

print("mean:", exact_mean, "Fraction")
print("fmean:", float_mean, "float")

assert exact_mean == Fraction(3, 20)
assert isinstance(exact_mean, Fraction)
assert isclose(float_mean, 0.15)
assert isinstance(float_mean, float)

Recorded output

mean: 3/20 Fraction
fmean: 0.15000000000000002 float

References: [1], [2]

Keep numeric representation consistent across a calculation

If later steps require exact rational values, keep the pipeline in `Fraction` and use `mean`. If a floating result is intended, `fmean` makes that conversion explicit. Mixing types across records can make behavior harder to predict; Python documents type support for the statistics module and warns that mixed collections may have undefined or implementation-dependent behavior. [1, 2]

The example covers only two rational inputs and the standard library implementation available in Python {platform.python_version()}. It does not benchmark either function or generalize precision behavior to Decimal, huge values, weighted means, or custom numeric types. Verify the documented input contract for the types used in a particular calculation. [1, 2]

References: [1], [2]

Sources

  1. Python: statistics.mean
  2. Python: statistics.fmean