Feature Scaling
Feature Scaling
Fit numeric transforms on training rows, then apply the same saved parameters to new rows. Use rectangular Python lists with rows as examples and columns as features; assume finite values, no missing entries, and compatible parameter lengths.
Functions to implement
min_max_scale(X)
Return (scaled_X,mins,maxs), computing each column's minimum and maximum from X. A nonconstant column transforms as (x-min)/(max-min). For a constant training column, output 0.0. Empty fitting input returns ([],[],[]).
min_max_transform(X,mins,maxs)
Apply the saved rule without recomputing parameters. If the saved range is zero, output 0.0 even if a new value differs from the training constant. Do not clip: held-out values can produce coordinates below zero or above one. This zero-range rule is the exercise's explicit policy, not a promise of equivalence to every library scaler.
standardize(X)
Return (standardized_X,means,stds). Compute each column's population variance with denominator n, not n-1. The returned stds are effective denominators: use sqrt(variance) when variance is positive and 1.0 when variance is zero. Transform with (x-mean)/stds[j]. Constant training columns become zero but do not have unit variance. Empty fitting input returns ([],[],[]).
standardize_transform(X,means,stds)
Apply (x-means[j])/stds[j] using the saved effective scales, which are positive. Do not refit. A changed value in a previously constant column retains its displacement from the training mean. Both transform functions return [] for no input rows.
Worked examples
scaled, mins, maxs = min_max_scale([[2,7],[4,7],[6,7]])
# scaled = [[0,0],[0.5,0],[1,0]], mins=[2,7], maxs=[6,7]
min_max_transform([[8,9]], mins, maxs) # [[1.5,0]]
scaled, means, stds = standardize([[1,7],[3,7]])
# scaled = [[-1,0],[1,0]], means=[2,7], stds=[1,1]
standardize_transform([[5,9]], means, stds) # [[3,2]]
Each feature is scaled independently. Save the returned vectors with the model; refitting them on validation or prediction rows changes the representation.