Apply Scaling to a New Row
Apply frozen training minimums and spans to an unseen observation whose values step outside the training interval.
The problem
In deployment, a trained model receives one new row at a time, and nothing prevents a new sensor reading or customer profile from exceeding the largest value seen during training or falling below the smallest. Suppose three training rows are [[10, 200], [30, 400], [50, 600]], and a new row arrives at [60, 100] (last row of input).
How should min-max scaling transform [60, 100]? Two tempting shortcuts fail here: recomputing and over all four rows shifts the coordinate system under the trained model's feet, while clamping outputs into erases how far outside the training box the new row sits.
Work through the numbers
Pin column minimums and spans on the three training rows [[10, 200], [30, 400], [50, 600]] alone. Column has , , and span . Column has , , and span .
Apply directly to the new row [60, 100]. In column , , placing one-quarter of a training span above . In column , , placing one-quarter of a training span below .
The exact scaled vector is [1.25, -0.25]. Neither refitting all four rows (which would return [1.0, 0.0]) nor clamping to (which would also return [1.0, 0.0]) preserves that 25 percent extrapolation on both axes.
The live model
A vertical zero line and reference frame show signed scaled bars for the new arrival as new_col0 sweeps from to (holding new_col1 at , scaled ). At default new_col0 , bars show [1.25, -0.25]. At new_col0 , column drops below zero to [-0.25, -0.25]; at it sits on the lower edge [0, -0.25]; at it reaches [0.5, -0.25]; at it hits [1.0, -0.25]; at it extends to [1.5, -0.25].
![Signed coordinate bars relative to [0, 1] training bounds showing col 0 at +1.25 and col 1 at -0.25 for new row [60, 100].](../assets/ml/apply-scaling-to-a-new-row--demo.png)
let new_col0 = scene.parameter("new_col0", 60.0, 0.0, 70.0, 10.0);
let n = input.size(0);
let train = input.slice(0, 0, n - 1);
let r0 = torch.full(1, new_col0).to(DType.F64);
let r1 = input.select(0, n - 1).slice(0, 1, 2);
let row = torch.cat(torch.list().add(r0).add(r1), 0);
let lo = train.amin(0);
let hi = train.amax(0);
let answer = (row - lo).div(hi - lo);
out.set("scaled_new_row", answer);
{
"scaled_new_row": [
1.25,
-0.25
]
}Watch the parameter change
Your turn
The starter program lets the incoming row redefine the feature bounds, returning [1.0, 0.0] instead of [1.25, -0.25]. Set answer to the un-clamped scaled feature vector for the final row of input using min-max bounds pinned strictly on the preceding training rows; Check tests unseen rows that sit below , inside , and above .
![Starter bars refitting min and max on the new row to return [1.0, 0.0] instead of [1.25, -0.25].](../assets/ml/apply-scaling-to-a-new-row--exercise.png)
let n = input.size(0);
let row = input.select(0, n - 1);
let lo = input.amin(0);
let hi = input.amax(0);
let answer = (row - lo).div(hi - lo);
out.set("scaled_new_row", answer);
{
"scaled_new_row": [
1,
0
]
}Pin column minimums and maximums on the training rows input[0..N-1] and return the un-clamped scaled vector for the new row input[N-1].
Read the result carefully
Why is a scaled value of or desirable rather than an error? A linear model trained on scaled coordinates learned a slope per unit of training span; feeding lets that linear rule extrapolate smoothly for , whereas clamping , , and all to makes the model blind to every value above .
At the same time, extreme values like or serve as an immediate out-of-distribution alert in production monitoring: any coordinate outside tells you at a glance that an incoming feature has left the training range.
Recap
- New rows are scaled with the frozen training vectors , never by updating or .
- Values above map cleanly above and values below map cleanly below (
[60, 100]becomes[1.25, -0.25]). - Clamping new scaled features to discards extrapolation signal and conflates distinct out-of-range inputs.