Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Exploring and Plotting Data  ›  Lesson

Spread and Outliers

Variance & Std. Deviation 15 minVariance and standard deviation measure how far values wander from the mean, and outliers can be flagged with IQR fences or z-scores before being investigated.

Spread and Outliers

Two branches can both average 3 copies a sale: one sells 3 every time, the other swings between 0 and 6. Same centre, very different shop to plan stock for. Spread measures how far values wander from the centre, and it is also how you spot values that wander too far.

The rule

Spread measures the wander; outliers sit beyond the fences.

Variance is the average squared distance from the mean. Standard deviation is its square root, which brings the answer back to the original units (copies, not copies squared).

sales["copies"].var()   # about 2.79
sales["copies"].std()   # about 1.67 copies

pandas divides by n − 1 (the sample version). NumPy's np.var divides by n unless you pass ddof=1, so the two can disagree on the same numbers: np.var gives 2.4375 here.

The interquartile range (IQR) measures the spread of the middle half. Q1 is the value a quarter of the way up the sorted data, Q3 three quarters up:

q1 = sales["copies"].quantile(0.25)   # 2.0
q3 = sales["copies"].quantile(0.75)   # 4.25
iqr = q3 - q1                          # 2.25

Two common ways to flag outliers:

  • IQR fences: anything below Q1 − 1.5 × IQR or above Q3 + 1.5 × IQR. Here the fences are −1.375 and 7.625, so no sale is flagged.
  • Z-score: (value − mean) / std, the number of standard deviations from the mean. Values beyond 3 (or −3) are flagged.
z = (sales["copies"] - sales["copies"].mean()) / sales["copies"].std()
outliers = sales[(sales["copies"] < q1 - 1.5 * iqr) | (sales["copies"] > q3 + 1.5 * iqr)]

And always look: a box plot (coming soon) marks values beyond these fences as dots.

The shortcut

Prefer the IQR fences on small or skewed data. An extreme value inflates the mean and std it is measured against, so it can hide from its own z-score. Quartiles barely notice it.

Catching the 60-copy bulk orderStep 1 of 4
  1. Add the bulk sale

    Copies become 3, 5, 2, 4, 1, 6, 2, 3, 60.

The trap is treating "outlier" as "error". Some are typos (600 instead of 6); some are your most important customers. Investigate before you drop.

Drills

Answer fast — trust the rule.

quick drill

Which measure is in the same units as the data itself?

quick drill

Q1 is 10 and Q3 is 18. What is the upper IQR fence?

quick drill

A value is 2 standard deviations below the mean. What is its z-score?

quick drill

Why did the 60-copy sale escape the z-score test?

Remember

  • Spread measures the wander; outliers sit beyond the fences.
  • Standard deviation is in the data's units; pandas uses n − 1, NumPy uses n by default.
  • Prefer IQR fences on small or skewed data, and investigate outliers before dropping them.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFlag, do not delete

Copies per sale are 3, 5, 2, 4, 1, 6, 2, 3 and one sale of 60. Explain how the IQR fences and the z-score each judge the 60, why they disagree, and what you would do with that sale.