Data Science
Z-Score Standardization
You take one pass to compute the mean, one more for the standard deviation, and one more to compute every z-score — O(n) time, O(n) space for the output.
The idea, in plain English
Say you scored 90 on a hard test where the average was 70, and also 90 on an easy test where the average was 85. Which 90 is more impressive? Raw scores cannot tell you, but z-scores can. A z-score answers one question: how many standard deviations away from the average is this value? A z-score of 0 means exactly average. A z-score of +2 means solidly above average. A z-score of -1 means a bit below average. Once you turn every value into a z-score, you can fairly compare numbers that come from completely different scales.
How it works
- 1Find the mean and the standard deviation of the whole dataset.
- 2For each value, subtract the mean. This tells you how far it is from average, in the original units.
- 3Divide that difference by the standard deviation. This rescales the distance into 'number of standard deviations', which is the z-score.
When you'd use it
Use z-scores whenever you need to compare values from different scales or different distributions, like comparing a student's math score to their reading score, or flagging unusually large transactions in fraud detection. This is also a common step before feeding data into models that expect roughly centered, evenly-scaled features.
Common beginner mistakes
- Do not compute the mean and standard deviation on the wrong dataset. Always compute them from the training data, then reuse those same two numbers to standardize the test data.
- Do not mix up z-score standardization (mean 0, spread measured in standard deviations) with min-max normalization (a fixed 0-to-1 range). They solve a similar problem in different ways.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
Data: 2 4 4 4 5 5 7 9
Mean: 5.00
Standard deviation: 2.00
Z-scores: -1.50 -0.50 -0.50 -0.50 0.00 0.00 1.00 2.00Not sure this is the right topic? See the learning paths → or where this leads →