Speed

Scale the Table

Broadcast 1D column minimums and spans across an entire N-by-D feature matrix and examine outlier compression.

The problem

Scaling one row at a time in a loop obscures the matrix structure of preprocessing. Given a four-row, three-column training table [[10, 100, 2], [20, 500, 8], [30, 300, 6], [50, 900, 10]], tensor broadcasting subtracts the 1D column-minimum vector from every row at once and divides by the 1D column-span vector in a single expression.

Two mistakes commonly corrupt full-table scaling: collapsing the whole matrix to one global scalar minimum and maximum instead of keeping one pair per column, and overlooking how one extreme value in a column compresses every other row in that column toward zero.

Work through the numbers

Compute per-column bounds down axis 00. Column 00 ([10, 20, 30, 50]) has lo0=10\mathrm{lo}_0 = 10 and span 50−10=4050 - 10 = 40. Column 11 ([100, 500, 300, 900]) has lo1=100\mathrm{lo}_1 = 100 and span 900−100=800900 - 100 = 800. Column 22 ([2, 8, 6, 10]) has lo2=2\mathrm{lo}_2 = 2 and span 10−2=810 - 2 = 8.

Broadcast X−lohi−lo\frac{X - \mathrm{lo}}{\mathrm{hi} - \mathrm{lo}} across all four rows. Row 00 sits at all three minimums and becomes [0, 0, 0]. Row 11 ([20, 500, 8]) becomes [1040,400800,68]=[\frac{10}{40}, \frac{400}{800}, \frac{6}{8}] = [0.25, 0.5, 0.75]. Row 22 ([30, 300, 6]) becomes [2040,200800,48]=[\frac{20}{40}, \frac{200}{800}, \frac{4}{8}] = [0.5, 0.25, 0.5]. Row 33 sits at all three maximums and becomes [1, 1, 1].

Now watch what happens to column 11 if row 33's raw value of 900900 jumps by an outlier_add offset. Adding 800800 raises column 11's maximum to 1,7001{,}700 (span 1,6001{,}600), halving rows 11 and 22 in column 11 from [0.5, 0.25] down to [0.25, 0.125]. Adding 2,4002{,}400 raises the maximum to 3,3003{,}300 (span 3,2003{,}200), squashing rows 11 and 22 to [0.125, 0.0625].

The live model

The grid draws one cell bar per entry of the 4×34 \times 3 scaled matrix. At default outlier_add =0= 0, interior rows 11 and 22 show distinct proportions [0.25, 0.5, 0.75] and [0.5, 0.25, 0.5] while rows 00 and 33 anchor [0, 0, 0] and [1, 1, 1]. Dragging outlier_add to 800800 and 2,4002{,}400 leaves columns 00 and 22 untouched while visibly compressing column 11's interior bars toward 00.

4 by 3 grid of unit bars showing scaled rows [0, 0, 0], [0.25, 0.5, 0.75], [0.5, 0.25, 0.5], and [1, 1, 1].
# Fix line width in scale-the-table--demo.lmc to stay well under 80 chars!
let outlier_add = scene.parameter("outlier_add", 0.0, 0.0, 2400.0, 800.0);
let z3 = "[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,1.0,0.0]]";
let bump = torch.tensor(z3, DType.F64);
let tbl = input + bump * outlier_add;
let lo = tbl.amin(0);
let hi = tbl.amax(0);
let answer = (tbl - lo).div(hi - lo);
out.set("scaled_table", answer);
{
  "scaled_table": [
    [
      0,
      0,
      0
    ],
    [
      0.25,
      0.5,
      0.75
    ],
    [
      0.5,
      0.25,
      0.5
    ],
    [
      1,
      1,
      1
    ]
  ]
}
Watch the parameter change

Your turn

The starter program collapses the entire matrix onto a single shared range, squashing columns 00 and 22 near zero instead of scaling each feature onto [0,1][0, 1]. Set answer to the N×DN \times D matrix where every column is independently min-max scaled across rows; Check tests both the 4×34 \times 3 table above and unseen tables of varied shapes and scales.

Starter 4 by 3 grid using global min 2 and max 900, leaving columns 0 and 2 squashed near zero.
let lo = input.min();
let hi = input.max();
let answer = (input - lo).div(hi - lo);
out.set("scaled_table", answer);
{
  "scaled_table": [
    [
      0.008908685968819599,
      0.1091314031180401,
      0
    ],
    [
      0.0200445434298441,
      0.5545657015590201,
      0.0066815144766146995
    ],
    [
      0.031180400890868598,
      0.33184855233853006,
      0.004454342984409799
    ],
    [
      0.053452115812917596,
      1,
      0.008908685968819599
    ]
  ]
}

Rescale every column of the 2D table input independently to [0, 1] using column-wise minimums and maximums.

Read the result carefully

Every non-constant column of a min-max scaled training matrix has exact minimum 0.00.0 and exact maximum 1.01.0: if any column's minimum is nonzero or its maximum falls below 1.01.0, the reduction axis is wrong. Watch out for a constant column as well: span hi−lo=0\mathrm{hi} - \mathrm{lo} = 0 makes x−lo0\frac{x - \mathrm{lo}}{0} undefined, so drop the constant column or map it explicitly to 00.

At the same time, the outlier_add slider reveals min-max scaling's structural weakness. Because only two extreme rows pin the denominator hi−lo\mathrm{hi} - \mathrm{lo}, a single sensor spike or billionaire income in training crowds all typical observations into a narrow sliver near zero.

This book teaches min-max scaling, while standardization — subtracting the training mean μ\mu and dividing by the training standard deviation σ\sigma via x−μσ\frac{x - \mu}{\sigma} — is the usual alternative and is far less squeezed by one extreme value. Its mean and standard deviation are pinned on training rows exactly like lo\mathrm{lo} and hi\mathrm{hi}; libraries differ in dividing variance by nn or n−1n - 1, so always state which divisor you use.

Recap

  • Tensor broadcasting min-max scales an N×DN \times D table in one step via X−lohi−lo\frac{X - \mathrm{lo}}{\mathrm{hi} - \mathrm{lo}} per column, provided no column is constant (hi−lo=0\mathrm{hi} - \mathrm{lo} = 0).
  • Global scalar bounds preserve cross-column unit imbalances instead of putting every feature on [0,1][0, 1].
  • When one extreme outlier squeezes min-max intervals, standardization (x−μσ\frac{x - \mu}{\sigma} pinned on training rows, stating nn versus n−1n - 1) is the standard alternative.