Calculating Z-Scores for Anomaly Detection
What you'll learn: How to compute Z-scores for each data point and use their magnitude to identify unusual observations.
The Core Idea
Now that you understand what the Z-Score Method is (measuring how many standard deviations away from the mean a point sits), let's actually calculate these scores and interpret them. A Z-score transforms your data into a standardized scale where you can objectively say "this point is unusually far from typical values."
Computing Z-Scores
The formula is straightforward:
Z = (X - μ) / σ
Where:
- X is your observation
- μ (mu) is the mean of your dataset
- σ (sigma) is the standard deviation
For each observation in your dataset, you subtract the mean and divide by the standard deviation. This tells you "how many standard deviations away is this point?"
Interpreting Magnitude
Think of Z-scores like measuring how far someone lives from city center in "standard neighborhoods":
- |Z| < 2: Normal range (≈95% of data falls here)
- |Z| = 2 to 3: Unusual, worth investigating
- |Z| > 3: Strong anomaly candidate (only ~0.3% of normal data)
A Z-score of +2.5 means the value is 2.5 standard deviations above average. A score of -3.1 means 3.1 standard deviations below average. The sign shows direction; the magnitude shows extremeness.
Why This Works for Anomaly Detection
By standardizing your data, you can set universal thresholds. A threshold of |Z| > 3 works whether you're analyzing temperatures in Celsius or financial transactions in dollars—the logic remains consistent.
Key Takeaway: Calculate Z-scores by subtracting the mean and dividing by standard deviation for each observation; values with |Z| > 2 or 3 are increasingly likely to be anomalies worth investigating.