2.1.5 · D4Data Preprocessing & Feature Engineering

Exercises — Min-max scaling and z-score normalization

2,884 words13 min readBack to topic

This page is a self-test ladder. Each problem is graded from L1 Recognition (do you know the definition?) up to L5 Mastery (can you invent and defend a scaling strategy?). Every problem has a fully worked solution hidden inside a collapsible callout — try first, then reveal.

Prerequisite reading: the parent topic note. If a term feels unfamiliar, that note builds it from zero.

Two formulas power every problem below. We restate them so no symbol is unearned.

Figure — Min-max scaling and z-score normalization

The figure above is the mental picture for everything: min-max slides and squeezes the ruler so its ends land on 0 and 1; z-score recenters the ruler on the mean and measures in units of spread.


Level 1 — Recognition

Problem 1.1

A feature has and . Using min-max scaling to , what scaled value does receive?

Recall Solution

WHAT: Plug into the min-max formula. WHY: is asked as a fraction of the way from the smallest to the largest value. WHAT IT LOOKS LIKE: is exactly halfway between and , so on the squeezed ruler it lands dead center at . ✅

Problem 1.2

For a z-score, what does mean? What does mean?

Recall Solution

: the value equals the mean exactly (, so numerator is ). : the value sits two standard deviations below the mean. The minus sign means "below"; the "2" means "two spreads away."


Level 2 — Application

Problem 2.1

Dataset: Heights (cm) . Min-max scale all five values to .

Recall Solution

Step 1 (WHAT): find the endpoints. , , range . Step 2 (WHY): each value's distance above , divided by , gives its position on the ruler.

Result: . Evenly spaced input stays evenly spaced — min-max is linear, it never bends the shape.

Problem 2.2

Dataset . Scale to the custom range . Use the generalized min-max formula stated in the opening callout.

Recall Solution

WHY this formula: we want the generalized map , where and are the endpoints of the target interval. Here the target is , so (low end) and (high end); the data endpoints are , . Read it as: map to first via , then stretch by and shift by .

  • (the middle value lands at the middle of the target range)

Result: .

Problem 2.3

Test scores . Compute the z-scores (population ).

Recall Solution

Step 1: mean . Step 2: variance . Step 3: . Step 4: apply :

Result: . Symmetric data gives symmetric z-scores.


Level 3 — Analysis

Problem 3.1

You fit a min-max scaler on training data with , . A test point arrives with value . What scaled value does it get, and why is that acceptable (not a bug)?

Recall Solution

Use the training min and max (never refit on test data): The output is , outside . This is expected and correct: the test value is larger than anything seen in training, so it lands past the right end of the ruler. Refitting the scaler on the test set (to force it back into ) would be the actual bug — it changes what a given raw value means and misaligns the model's learned boundaries.

Problem 3.2

Salaries (thousands). Compute the min-max scaled value of and its z-score. Which method keeps the four "normal" salaries more spread out, and why?

Recall Solution

Min-max: , , range . The four normal salaries () all crush into — the outlier stole the whole ruler.

Z-score: mean . Variance . .

Which spreads normals more? Z-score. Under min-max the normal points span a width of only ; under z-score they span roughly — a width near , still small but relatively larger, and crucially the outlier is only (not the maximum of the ruler). Min-max is defined by the extremes, so a single outlier controls it; z-score is defined by mean and spread, which one point moves less violently.

The figure below makes the crush visible. Look at the top ruler (min-max): the four normal salaries (black dots) pile up against the left edge near , while the red outlier at k sits alone at and owns the entire scale. Now the bottom ruler (z-score): the same four black dots stay visibly separated and the red outlier is only about , not the extreme end. This is the whole argument for preferring z-score (or robust scaling) when a lone extreme value is present.

Figure — Min-max scaling and z-score normalization

Level 4 — Synthesis

Problem 4.1

Prove algebraically that any value scaled by min-max to always lands within for the training data, i.e. show when (and the range is strictly positive).

Recall Solution

We assume (a strictly positive range; the constant-feature case with zero range was handled separately in the opening callout, where we define the output as ). Start from . Subtract from all three parts: The right end is the range, which is by assumption. Divide all three parts by the positive range (dividing an inequality by a positive number preserves its direction): The middle is exactly , so . ∎ The guarantee holds only for values inside the training range — Problem 3.1 showed why test data can escape it.

Problem 4.2

A pipeline first min-max scales a feature to , then applies z-score normalization to that scaled feature. Show that the final z-scores are identical to z-scoring the raw feature directly. (Take raw data .)

Recall Solution

Raw z-score: . , so . z-scores: , , .

Min-max first: , range . Scaled . Now z-score the scaled data: mean . Deviations: ; squares sum = , . z-scores: , , . Identical.

WHY: min-max is a linear map with . Z-scoring subtracts the mean and divides by the standard deviation, and both mean and standard deviation scale by exactly the same factor under a linear map — so the constant (and shift ) cancel completely. Composing an affine map before standardization changes nothing.


Level 5 — Mastery

Problem 5.1

You are building a k-NN classifier (distance-based) on two features: income (range , right-skewed with a few billionaires) and age (, roughly symmetric). Choose and justify a scaling strategy for each feature. State what would go wrong with the naive choice.

Recall Solution

The requirement: k-NN measures Euclidean distance, so both features must contribute comparably; a feature with a huge numeric span dominates the distance and silently decides every neighbor.

age: roughly symmetric, bounded, no extreme outliers → z-score (or min-max) is fine. Z-score is a clean default: centers at , spread .

income: heavily right-skewed with extreme outliers (billionaires). Naive min-max would set ; every ordinary income crushes into a tiny sliver near (exactly the Problem 3.2 failure). Naive z-score is less bad but the outliers still inflate , compressing the normal range. Best: apply a log transform first (to tame the skew), then standardize — or use robust scaling which uses the median and interquartile range and ignores the tails.

What goes wrong with the naive choice (min-max both features): income's scaled distances become nearly for all normal people, so k-NN effectively classifies on age alone. The model looks trained but ignores half its inputs.

Bonus consistency note: whatever you pick, fit on train, transform test (Problem 3.1), and remember scaling is irrelevant for tree models — only distance/gradient methods like k-NN and gradient descent need it.

Problem 5.2

Design a single formula that maps a feature to but is robust to a known upper outlier, by clipping at the 95th percentile instead of the true max. It must also never produce a negative output (values below , e.g. new low test data, must be pinned at ). Write it, and compute the mapped value of when and . What happens to ? What happens to ?

Recall Solution

Formula (robust, clipped both ends): first form the raw ratio, then clamp it into from both sides using a nested : WHY the outer : anything above gives a ratio ; capping at stops the lone upper outlier from stretching the whole ruler. WHY the inner : anything below (a new, unusually low value) gives a negative ratio; the inner pins it to so the output can never go negative — the lower edge case is now explicitly handled.

Compute : ; both clamps are inactive, so . Normal values keep full resolution.

Compute : raw ratio , but . The upper outlier is capped at .

Compute : raw ratio , but , then . The below-range value is pinned to instead of going negative. This two-sided clamping is the core idea behind robust scaling — full treatment in the robust-scaling note.


Recall Self-test summary (cloze)

Min-max maps 0 and 1. Z-score gives mean 0 and standard deviation 1. The method most damaged by a single extreme outlier is min-max. Always fit on train, transform test to keep the scale consistent. Composing a linear pre-scale before z-scoring changes the result by nothing (they cancel). When the range is zero (constant feature), define the output as 0 (and usually drop the feature).

Which method's output is guaranteed to lie in for training data?
Min-max scaling.
What does a z-score of mean?
The value is 1.5 standard deviations below the mean.
Why does one outlier ruin min-max?
It becomes , inflating the range so all other values compress toward 0.
What do you use from when transforming test data?
The training set (never refit on test).
What do you do when ?
Avoid dividing by zero — define the scaled output as 0 (constant feature carries no information).