Speed

Scale One Row

Transform a single observation into [0, 1] feature coordinates by subtracting pinned column minimums and dividing by column spans.

The problem

Once column minimums and maximums are pinned on a training table, how does min-max scaling convert one raw observation into unitless [0,1][0, 1] coordinates? Consider the four-row training table [[10, 100], [20, 250], [30, 350], [50, 600]] and pick row_index =1= 1, whose raw values are [20, 250].

Dividing [20, 250] directly by the column maximums [50, 600] gives [0.4, 0.4167], which fails to map the minimum row [10, 100] to zero. Exact min-max scaling measures how far a value sits above its column minimum as a fraction of the column's span: x−lohi−lo\frac{x - \mathrm{lo}}{\mathrm{hi} - \mathrm{lo}}.

Work through the numbers

First read the pinned column bounds from [[10, 100], [20, 250], [30, 350], [50, 600]]. Column 00 has lo0=10\mathrm{lo}_0 = 10, hi0=50\mathrm{hi}_0 = 50, and span 50−10=4050 - 10 = 40. Column 11 has lo1=100\mathrm{lo}_1 = 100, hi1=600\mathrm{hi}_1 = 600, and span 600−100=500600 - 100 = 500.

Now transform row_index =1= 1 ([20, 250]) coordinate by coordinate. In column 00, compute 20−1050−10=1040=0.25\frac{20 - 10}{50 - 10} = \frac{10}{40} = 0.25. In column 11, compute 250−100600−100=150500=0.3\frac{250 - 100}{600 - 100} = \frac{150}{500} = 0.3. Row 11 maps to [0.25, 0.3].

Check the remaining rows with the same formula. Row 00 ([10, 100]) sits at both minimums and maps to [0, 0]. Row 22 ([30, 350]) sits halfway across both spans (20/4020 / 40 and 250/500250 / 500) and maps to [0.5, 0.5]. Row 33 ([50, 600]) sits at both maximums and maps to [1, 1].

The live model

The bars display the selected row's scaled coordinates in [0,1][0, 1] alongside reference ticks at 00, 0.50.5, and 1.01.0. At default row_index =1= 1, the bars stand at [0.25, 0.3]. Dragging row_index to 00 drops both coordinates to [0, 0]; dragging to 22 centers both at [0.5, 0.5]; dragging to 33 fills both unit bars to [1, 1].

Unit-interval gauges for row_index 1 ([20, 250]) showing scaled feature proportions 0.25 and 0.3.
let row_index = scene.parameter("row_index", 1.0, 0.0, 3.0, 1.0);
let lo = input.amin(0);
let hi = input.amax(0);
let row = input.select(0, int(row_index));
let answer = (row - lo).div(hi - lo);
out.set("scaled_row", answer);
{
  "scaled_row": [
    0.25,
    0.3
  ]
}
Watch the parameter change

Your turn

The starter program returns [0.4, 0.4167] on row 11 instead of [0.25, 0.3] and never maps the minimum row to [0, 0]. Set answer to the min-max scaled coordinate vector for row row_index using input's per-column minimums and maximums; Check tests different selected row indices and unseen multi-column tables.

Starter gauges dividing row 1 by max only ([0.4, 0.42]) instead of min-max proportions [0.25, 0.3].
let row_index = scene.parameter("row_index", 1.0, 0.0, 3.0, 1.0);
let hi = input.amax(0);
let row = input.select(0, int(row_index));
let answer = row.div(hi);
out.set("scaled_row", answer);
{
  "scaled_row": [
    0.4,
    0.4166666666666667
  ]
}

Select row int(row_index) from input and return its min-max scaled coordinate vector using input's column minimums and maximums.

Read the result carefully

Notice the geometric meaning of [0.25, 0.3]: row 11 sits 25 percent of the way from minimum to maximum in feature 00, and 30 percent of the way from minimum to maximum in feature 11. Both numbers are now pure proportions with no physical units attached; multiplying column 11 by 1,0001{,}000 rescales lo\mathrm{lo}, hi\mathrm{hi}, and xx together, canceling out completely in 250,000−100,000600,000−100,000=0.3\frac{250{,}000 - 100{,}000}{600{,}000 - 100{,}000} = 0.3.

Notice also why shifting the numerator without shifting the denominator is still wrong: 20−1050=0.2\frac{20 - 10}{50} = 0.2 would map even the maximum row 50−1050=0.8\frac{50 - 10}{50} = 0.8 short of 1.01.0 whenever lo≠0\mathrm{lo} \ne 0.

Recap

  • Min-max scaling maps one row xx via x−lohi−lo\frac{x - \mathrm{lo}}{\mathrm{hi} - \mathrm{lo}}, using per-column minimums lo\mathrm{lo} and maximums hi\mathrm{hi}.
  • Every coordinate becomes a unitless fraction of its column's observed training span, placing the minimum at 00 and the maximum at 11.
  • Rescaling a column by any positive unit factor leaves its min-max coordinates unchanged because numerator and denominator scale together.