Distance Changes After Scaling
Compare Euclidean nearest-neighbor rankings before and after min-max scaling to see how wide-span features hijack distance.
The problem
Distance-based algorithms such as -nearest neighbors add squared differences across columns: . When one column spans units and another spans units, a tiny percentage difference in the wide column outweighs a complete end-to-end difference in the narrow column.
Suppose three stored profiles [years_experience, salary_k] are [1, 100], [5, 120], and [2, 200], and a query arrives at [1, 125] (last row of input). Which stored profile is closest before scaling, and which is closest after pinning min-max scaling on the three stored profiles?
Work through the numbers
First compute raw squared distances from [1, 125]. Against [1, 100], . Against [5, 120], . Against [2, 200], . Unscaled distances [625, 41, 5626] crown [5, 120] the winner, even though sits at the opposite extreme of experience ( years versus ).
Now pin min-max bounds on the three stored rows [[1, 100], [5, 120], [2, 200]]: , , and . The scaled stored rows become , , and , while the query [1, 125] maps to .
Recompute squared distances in coordinates. To : . To : . To : . Scaled distances [0.0625, 1.0025, 0.625] flip the ranking completely: wins at and drops to last place!
The live model
Horizontal bars show the scaled squared distance from the query to each stored candidate, highlighting the nearest neighbor in green. At default query_col1 , distances are [0.0625, 1.0025, 0.625] and candidate wins. At query_col1 , distances become [0, 1.04, 1.0625] (candidate wins); at they are [0.25, 1.09, 0.3125] (candidate still wins); at they shift to [0.5625, 1.3025, 0.125] and candidate takes first place.
![Distance bars for 3 stored candidates showing [0.0625, 1.0025, 0.625] with candidate 0 highlighted green as nearest.](../assets/ml/distance-changes-after-scaling--demo.png)
let query_col1 = scene.parameter("query_col1", 125.0, 100.0, 200.0, 25.0);
let n = input.size(0);
let stored = input.slice(0, 0, n - 1);
let q0 = input.select(0, n - 1).slice(0, 0, 1);
let q1 = torch.full(1, query_col1).to(DType.F64);
let query = torch.cat(torch.list().add(q0).add(q1), 0);
let lo = stored.amin(0);
let span = stored.amax(0) - lo;
let s_scaled = (stored - lo).div(span);
let q_scaled = (query - lo).div(span);
let answer = (s_scaled - q_scaled).square().sum(1);
out.set("scaled_sq_distances", answer);
{
"scaled_sq_distances": [
0.0625,
1.0025,
0.625
]
}Watch the parameter change
Your turn
The starter program evaluates distances in raw units ([625, 41, 5626]), choosing candidate instead of candidate . Set answer to the vector of squared Euclidean distances from the final query row to each preceding stored row after min-max scaling with bounds pinned strictly on the stored rows; Check varies stored reference tables, column counts, and out-of-range queries.
![Starter bars with raw squared distances [625, 41, 5626], picking candidate 1 instead of candidate 0.](../assets/ml/distance-changes-after-scaling--exercise.png)
let n = input.size(0);
let stored = input.slice(0, 0, n - 1);
let query = input.select(0, n - 1);
let answer = (stored - query).square().sum(1);
out.set("scaled_sq_distances", answer);
{
"scaled_sq_distances": [
625,
41,
5626
]
}Pin min-max bounds on the stored rows input[0..N-1], rescale both those stored rows and the query row input[N-1], and return squared Euclidean distances from the scaled query.
Read the result carefully
Look at why [5, 120] lost once features shared bounds. On the experience axis, versus is a 100 percent span gap (squared difference ), whereas on the salary axis, versus is a 25 percent span gap (squared difference ). Raw units weighted salary unit equally with experience year, inflating salary's influence by times in squared distance.
Notice also the pipeline boundary: and come from the stored reference table, never from the incoming query row. If a query arrives with salary , its scaled coordinate participates honestly in distance without compressing the stored points.
Recap
- Unscaled Euclidean distance weights each column by the square of its unit span, letting wide-range columns dictate nearest neighbors.
- Rescaling stored rows and the query with stored-set bounds puts every full-span difference on an equal footing, flipping
[625, 41, 5626]to[0.0625, 1.0025, 0.625]. - Query points are transformed using the stored table's pinned bounds rather than included when computing and .