Neural Mastery
← Back to Practice

Standardize a Dataset (Z-Score Normalization)

Difficulty: Medium · Concept: Probability & Statistics — Expectation, Variance, Covariance

A direct, higher-stakes application of Sample Mean and Variance: z-score normalization rescales every value to "how many standard deviations from the mean is this," zi=xixˉσz_i = \frac{x_i - \bar{x}}{\sigma}. This is the standard feature-scaling step before training many models — gradient descent converges faster and more reliably when features share a common scale, and distance-based methods like K-Means or KNN give distorted results if one feature's raw scale dwarfs another's.

The genuine edge case: standardization is undefined when a dataset has zero variance (every value identical) — dividing by a standard deviation of 0 has no sensible answer, and a real implementation needs to fail loudly rather than produce inf/nan silently.

Your task: implement standardize(data), returning a list of z-scores the same length as data. Raise ValueError if the data has zero variance.

Implement it yourself
assert standardize([2, 4, 4, 4, 5, 5, 7, 9]) == [-1.5, -0.5, -0.5, -0.5, 0.0, 0.0, 1.0, 2.0] assert all(abs(a - b) < 1e-9 for a, b in zip(standardize([1, 2, 3, 4, 5]), [-1.4142135623730951, -0.7071067811865476, 0.0, 0.7071067811865476, 1.4142135623730951])) assert all(abs(a - b) < 1e-9 for a, b in zip(standardize([-2, -1, 0, 1, 2]), [-1.4142135623730951, -0.7071067811865476, 0.0, 0.7071067811865476, 1.4142135623730951])) assert check_raises(ValueError, standardize, [10, 10, 10]) == True

Next: Bayes' Theorem: Posterior From Scratch

Last updated Sep 5, 2026Edit this pageReport an issue