Speed

Scale the Train Rows

Transform training features onto [0, 1] by subtracting tr_min and dividing by tr_span = tr_max - tr_min while keeping held-out rows masked.

The problem

In Fit Min and Max on Train, we locked in tr_min, tr_max, and tr_span = tr_max - tr_min from role == 1 rows alone. Now we use that ruler to place every training row onto the unit interval [0, 1] before fitting weights.

Two arithmetic pieces must happen in order: shift the lowest training point down to 0 by subtracting tr_min, then divide that distance by tr_span—not by tr_max—so the highest training point lands squarely on 1.

Work through the numbers

Take five [role, x] rows in table = [[1, 10], [1, 15], [0, 50], [1, 25], [1, 30]]. Training rows (role = 1) take values [10, 15, 25, 30], so tr_min = 10, tr_max = 30, and tr_span = 30 - 10 = 20. Held-out row r2 = 50 (role = 0) stays masked at 0 until the test transform step.

Evaluating (x - 10) / 20 across the four training rows places each one at its exact fractional position inside [10, 30]:

  • r0 (x = 10): (10 - 10) / 20 = 0 / 20 = 0
  • r1 (x = 15): (15 - 10) / 20 = 5 / 20 = 0.25
  • r2 (role = 0, x = 50): masked by tr_mask = 0 -> 0
  • r3 (x = 25): (25 - 10) / 20 = 15 / 20 = 0.75
  • r4 (x = 30): (30 - 10) / 20 = 20 / 20 = 1

That produces scaled_train = [0, 0.25, 0, 0.75, 1]. Notice why dividing by tr_max = 30 fails whenever tr_min > 0: (30 - 10) / 30 = 20 / 30 = 0.6667, stopping short of 1.

The live model

Blue bars fill the [0, 1] track for every role == 1 row while r2 remains masked in amber. At default r1_x = 15, r1 fills one quarter of the track (0.25). Slide r1_x down to 10 to touch 0, across 20 (0.5) and 25 (0.75), and up to 30 to hit 1, watching r1 sweep smoothly across [0, 1] without changing tr_min = 10 or tr_span = 20.

Training rows [10, 15, 25, 30] mapped onto [0, 0.25, 0.75, 1] using (x - 10) / 20 while held-out r2 = 50 stays masked.
let r1_x = scene.parameter("r1_x", 15.0, 10.0, 30.0, 5.0);
let table = input.clone();
table.select(0, 1).set(1, r1_x);
let role = table.select(1, 0);
let x = table.select(1, 1);
let hi = x * 0.0 + x.max() + 1.0;
let lo = x * 0.0 + x.min() - 1.0;
let is_tr = role.eq(1.0);
let tr_mask = is_tr.to(DType.F64);
let tr_min = torch.where(is_tr, x, hi).min();
let tr_max = torch.where(is_tr, x, lo).max();
let tr_span = tr_max - tr_min;
let answer = (x - tr_min).div(tr_span) * tr_mask;
out.set("tr_min", tr_min).set("tr_span", tr_span).set("scaled_train", answer);
{
  "tr_min": 10,
  "tr_span": 20,
  "scaled_train": [
    0,
    0.25,
    0,
    0.75,
    1
  ]
}

Your turn

Observe row r4 (x = 30, the highest training reading) in the starter diagram: its bar stops two-thirds of the way across the unit track at 0.67, and r1 sits at 0.17 instead of 0.25. Fix the scaling denominator so the training rows stretch cleanly from 0 at r0 to 1 at r4 while r2 stays masked at 0. Check grades on a four-row table and a five-row table.

Dividing by tr_max = 30 instead of tr_span = 20 stops r4 = 30 at 0.67 instead of 1.0.
let table = input;
let role = table.select(1, 0);
let x = table.select(1, 1);
let hi = x * 0.0 + x.max() + 1.0;
let lo = x * 0.0 + x.min() - 1.0;
let is_tr = role.eq(1.0);
let tr_mask = is_tr.to(DType.F64);
let tr_min = torch.where(is_tr, x, hi).min();
let tr_max = torch.where(is_tr, x, lo).max();
let answer = (x - tr_min).div(tr_max) * tr_mask;
out.set("scaled_train", answer);
{
  "scaled_train": [
    0,
    0.16666666666666666,
    0,
    0.5,
    0.6666666666666666
  ]
}

Rescale the training rows onto the unit interval [0, 1] in answer while keeping held-out slots masked at 0.

Using the training interval width stretches the bars to [0, 0.25, 0, 0.75, 1], placing r0 on 0 and r4 squarely on 1.

Read the result carefully

Both endpoints are guaranteed on every training set with tr_max > tr_min: (tr_min - tr_min) / tr_span = 0 at the bottom of the training range and (tr_max - tr_min) / tr_span = 1 at the top, regardless of where tr_min sits relative to zero.

Recap

  • Min-max scaling maps training rows via (x - tr_min) / (tr_max - tr_min), placing tr_min at 0 and tr_max at 1.
  • Dividing by tr_max (30) instead of tr_span (20) compresses the upper training endpoint to (tr_max - tr_min) / tr_max whenever tr_min != 0.
  • Held-out rows (r2 = 50) play zero part in fitting [tr_min, tr_max] or scaling the training matrix.