Speed

The Role Column

Partition a table into complementary, non-overlapping training and held-out binary masks using an explicit role column attached to each row.

The problem

Evaluating a predictive rule on the same rows that shaped its parameters rewards memorization rather than generalization. Before computing a single average, weight, or error score, every row in a dataset needs an unambiguous assignment: either it is allowed to inform fitting (train) or it is locked away for honest evaluation (held-out test).

Slicing "the first four rows" works only while rows stay in one fixed order; sorting by date or customer ID scrambles positional slices immediately. Carrying an explicit role column (1 for train, 0 for held-out) binds split membership directly to each observation regardless of row order.

Work through the numbers

Consider six observations stored as [role, y]: [1, 12], [1, 18], [0, 25], [1, 15], [0, 31], and [1, 19]. Notice that held-out rows appear at indices 2 and 4, interleaved among training rows 0, 1, 3, and 5.

Testing role == 1 builds the binary training mask tr_mask = [1, 1, 0, 1, 0, 1], whose column sum is n_train = 4. Testing role == 0 builds the complementary held-out mask te_mask = [0, 0, 1, 0, 1, 0], whose column sum is n_test = 2. Stacking the two masks forms the 6-by-2 partition matrix [[1, 0], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0]]. Every row sums to 1 (tr_mask + te_mask = 1) and every elementwise product equals 0 (tr_mask * te_mask = 0).

The live model

The grid shows Role, Target y, tr_mask, and te_mask across all six rows, highlighting training membership in blue and held-out membership in amber. At default seed = 0, the deterministic draw holds out r2 and r4 (role = [1, 1, 0, 1, 0, 1]), giving n_train = 4 and n_test = 2.

Slide seed to 1 to hold out r1 and r3 ([1, 0, 1, 0, 1, 1]), or to 2 to hold out r0 and r2 ([0, 1, 0, 1, 1, 1]): each seed draws a fresh 4-and-2 partition with zero overlap.

Six rows partitioned by seed 0 into four training rows and two held-out test rows (r2 and r4) with zero overlap.
let seed = scene.parameter("seed", 0.0, 0.0, 2.0, 1.0);
let table = input.clone();
let s = int(seed);
for (r in range(0, table.size(0))) {
  let h = 3 * r + 3 * s + 2;
  let slot = h - int(floor(float(h) / 7.0)) * 7;
  let rv = 1.0;
  if (slot < 2) { rv = 0.0; }
  table.set(r * 2, rv);
}
let role = table.select(1, 0);
let tr_mask = role.eq(1.0).to(DType.F64);
let te_mask = role.eq(0.0).to(DType.F64);
let answer = torch.stack(torch.list().add(tr_mask).add(te_mask), 1);
out.set("n_train", tr_mask.sum()).set("masks", answer);
{
  "n_train": 4,
  "masks": [
    [
      1,
      0
    ],
    [
      1,
      0
    ],
    [
      0,
      1
    ],
    [
      1,
      0
    ],
    [
      0,
      1
    ],
    [
      1,
      0
    ]
  ]
}

Your turn

Four red cells light up in the starter's second column because every training row has also been admitted into te_mask, pushing the column totals to 4 and 6 on a six-row table. Repair te_mask so each row belongs to one split alone and the partition totals return to n_train = 4 and n_test = 2.

The starter sets te_mask to 1 on every row so training rows leak into the held-out mask.
let table = input;
let role = table.select(1, 0);
let tr_mask = role.eq(1.0).to(DType.F64);
let te_mask = role * 0.0 + 1.0;
let answer = torch.stack(torch.list().add(tr_mask).add(te_mask), 1);
out.set("n_train", tr_mask.sum()).set("masks", answer);
{
  "n_train": 4,
  "masks": [
    [
      1,
      1
    ],
    [
      1,
      1
    ],
    [
      0,
      1
    ],
    [
      1,
      1
    ],
    [
      0,
      1
    ],
    [
      1,
      1
    ]
  ]
}

Build the two-column partition matrix [tr_mask, te_mask] so every row belongs to either training (role 1) or held-out evaluation (role 0) with zero overlap.

Once only held-out rows enter te_mask, the red overlap cells clear and answer becomes [[1, 0], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0]].

Read the result carefully

Two algebraic invariants govern every valid two-way split: completeness (tr_mask + te_mask = 1 on all rows included in the experiment) and disjointness (tr_mask * te_mask = 0 everywhere). Violating disjointness leaks fitted rows into evaluation; violating completeness silently drops rows from both fitting and scoring. This book takes the role column as given. In practice it is drawn before anything else is computed: shuffle the row indices with a fixed random seed so the split can be repeated, and give the first share (often 80 percent) the role train.

When one class is rare, split each class separately so both roles keep its proportion; when rows belong together (several visits of one patient) or follow one another in time, assign whole groups or the latest period to held-out, because a random shuffle would put near-copies of a training row on the held-out side.

Every calculation in the rest of this book—training means, frozen baselines, held-out residuals, and MAE—multiplies by one of these two masks so interleaved row order never causes a mistake.

Recap

  • Store split assignment as an explicit role column (1 = train, 0 = held-out) so row order never alters which observations fit or score the model.
  • Derive tr_mask (role == 1) and te_mask (role == 0) as complementary 0/1 vectors satisfying tr_mask + te_mask = 1 and tr_mask * te_mask = 0.
  • Sum each mask (n_train = 4, n_test = 2) to obtain exact denominators for training fits and held-out metrics.