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 copiespandas 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.25Two common ways to flag outliers:
- IQR fences: anything below
Q1 − 1.5 × IQRor aboveQ3 + 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.