Speed

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 kk-nearest neighbors add squared differences across columns: d2(a,b)=∑j(aj−bj)2d^2(a, b) = \sum_j (a_j - b_j)^2. When one column spans 100100 units and another spans 44 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 r0=r_0 = [1, 100], r1=r_1 = [5, 120], and r2=r_2 = [2, 200], and a query arrives at q=q = [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 q=q = [1, 125]. Against r0=r_0 = [1, 100], 02+(−25)2=6250^2 + (-25)^2 = 625. Against r1=r_1 = [5, 120], 42+(−5)2=16+25=414^2 + (-5)^2 = 16 + 25 = 41. Against r2=r_2 = [2, 200], 12+752=1+5,625=5,6261^2 + 75^2 = 1 + 5{,}625 = 5{,}626. Unscaled distances [625, 41, 5626] crown r1=r_1 = [5, 120] the winner, even though r1r_1 sits at the opposite extreme of experience (55 years versus 11).

Now pin min-max bounds on the three stored rows [[1, 100], [5, 120], [2, 200]]: lo=[1,100]\mathrm{lo} = [1, 100], hi=[5,200]\mathrm{hi} = [5, 200], and span=[4,100]\mathrm{span} = [4, 100]. The scaled stored rows become s0=[0,0]s_0 = [0, 0], s1=[1.0,0.2]s_1 = [1.0, 0.2], and s2=[0.25,1.0]s_2 = [0.25, 1.0], while the query [1, 125] maps to qscaled=[0,0.25]q_{\mathrm{scaled}} = [0, 0.25].

Recompute squared distances in [0,1][0, 1] coordinates. To s0s_0: 02+(0−0.25)2=0.06250^2 + (0 - 0.25)^2 = 0.0625. To s1s_1: (1−0)2+(0.2−0.25)2=1+0.0025=1.0025(1 - 0)^2 + (0.2 - 0.25)^2 = 1 + 0.0025 = 1.0025. To s2s_2: (0.25−0)2+(1−0.25)2=0.0625+0.5625=0.625(0.25 - 0)^2 + (1 - 0.25)^2 = 0.0625 + 0.5625 = 0.625. Scaled distances [0.0625, 1.0025, 0.625] flip the ranking completely: r0r_0 wins at 0.06250.0625 and r1r_1 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 =125= 125, distances are [0.0625, 1.0025, 0.625] and candidate 00 wins. At query_col1 =100= 100, distances become [0, 1.04, 1.0625] (candidate 00 wins); at 150150 they are [0.25, 1.09, 0.3125] (candidate 00 still wins); at 175175 they shift to [0.5625, 1.3025, 0.125] and candidate 22 takes first place.

Distance bars for 3 stored candidates showing [0.0625, 1.0025, 0.625] with candidate 0 highlighted green as nearest.
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 11 instead of candidate 00. 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.
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 r1=r_1 = [5, 120] lost once features shared [0,1][0, 1] bounds. On the experience axis, 55 versus 11 is a 100 percent span gap (squared difference 1.01.0), whereas on the salary axis, 100100 versus 125125 is a 25 percent span gap (squared difference 0.06250.0625). Raw units weighted 11 salary unit equally with 11 experience year, inflating salary's influence by (100/4)2=625(100 / 4)^2 = 625 times in squared distance.

Notice also the pipeline boundary: lo\mathrm{lo} and hi\mathrm{hi} come from the stored reference table, never from the incoming query row. If a query arrives with salary 250250, its scaled coordinate 1.51.5 participates honestly in distance without compressing the stored points.

Recap

  • Unscaled Euclidean distance d2(a,b)=∑j(aj−bj)2d^2(a, b) = \sum_j (a_j - b_j)^2 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 [lo,hi][\mathrm{lo}, \mathrm{hi}] bounds puts every full-span difference on an equal 1.01.0 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 lo\mathrm{lo} and hi\mathrm{hi}.