4 · Mean, median & mode
Three ways to ask 'what is a typical value?' — and why the right answer depends on how skewed your data is.
Central tendency is not one number — it's a question: which average survives your data's outliers?
Without this:
Without distinguishing mean from median, you can't choose between MSE and MAE — two of the most common loss functions in ML.
Suppose someone asks: "what is a typical salary at this company?" The question sounds simple, but the answer depends on how you define 'typical'. There are three classic definitions:
- Mean (arithmetic average): sum all values, divide by n. Pulls toward extreme values — one billionaire in the room and the 'average' salary looks great.
- Median: sort all values, pick the middle one (or average of the two middle for even n). Robust to extremes — the billionaire barely moves it.
- Mode: the most frequently occurring value. Especially relevant for discrete or categorical data — the most-bought product, the most-common rating.
Each is a different answer to "what's the centre?" and each is appropriate in different situations. Using the mean on a heavily skewed dataset is one of the most common (and consequential) mistakes in data analysis.
Python (in browser)
A single extreme outlier inflates the mean by 633 % while leaving the median untouched. This is the core reason skewed distributions require the median.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
np.average(values, weights=...) computes the weighted mean. Weights encode the relative importance of each observation — essential for class-imbalanced evaluation.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
The trimmed mean lives between the mean (sensitive) and median (robust): remove the most extreme p% on each tail before averaging. The geometric mean averages compounded growth; the harmonic mean averages rates.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
On a right-skewed dataset like household income (most households earn moderate income, a few earn millions), which is typically larger — the mean or the median?
- **Mean** is sensitive to outliers; **median** is robust; **mode** suits discrete/categorical data. Choose based on the distribution's shape.
- For skewed distributions (income, latency, prices), the median tells the 'typical' story better. The mean tells the 'total resource' story better (useful for budgeting).
- **Trimmed mean** (`scipy.stats.trim_mean`) is a robust compromise. **Weighted mean** (`np.average(x, weights=w)`) is essential when observations have unequal importance.
Loss functions are means: MSE = mean of squared errors (tracks the arithmetic mean of squared residuals). Robust ML reaches for the median: MAE / Median Absolute Deviation perform better when outlier labels or outlier inputs are present in training data.
If you remove it: You can't choose between MSE and MAE without knowing which 'central tendency' the loss tracks — MSE optimizes the conditional mean, MAE optimizes the conditional median.