Paul's Python CoursePPC

Distributions, quartiles and outliers

Not met yet

A distribution is how readings are spread out, and a histogram shows its shape. Daily rain is skewed right: most days pile up near zero and a few wet ones stretch away in a long tail, which drags the mean out past the median. values.quantile(0.25) is the value a quarter of the way up the sorted readings; the first and third quartiles hold the middle half between them. One rule for outliers sets fences one and a half middle-halves beyond the quartiles and flags anything outside. It's a question, not a verdict: a stuck sensor is an error, but in a skewed distribution plenty of real days fall outside too.

Try it

main.py
import pandas as pd

melt = pd.Series([12, 14, 13, 15, 12, 58, 14, 13, 16])
q1, q3 = melt.quantile(0.25), melt.quantile(0.75)
low, high = q1 - 1.5 * (q3 - q1), q3 + 1.5 * (q3 - q1)
print(q1, q3, low, high)
print(melt[(melt < low) | (melt > high)].tolist())

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks every reading outside the fences is a mistake to delete. In a skewed distribution many real readings fall outside, and each needs a reason before it goes.
  • Thinks a distribution skewed to the right has most of its readings on the right. The tail is on the right; most readings sit at the left.
  • Thinks quantile(0.25) is a quarter of the largest value, rather than the value a quarter of the way up the sorted readings.

Where it comes up