Skip to main content

๐Ÿ“ Data Normalization

Descriptionโ€‹

< What is it? >โ€‹

  • [The idea]
    • Normalization (feature scaling) rewrites each input feature onto a common scale before training. It is a preprocessing step, not a model choice: nothing about what the model can express changes, only the numerical conditioning of the problem it is handed.
    • Raw features arrive in whatever units the world used โ€” age in years (0โ€“100), income in dollars (0โ€“500,000), a ratio in [0, 1]. Those units are arbitrary, but most learning algorithms are not invariant to them, so the units silently become weights. normalize data
  • [Why it matters]
    • Gradient descent geometry. With features on wildly different scales the loss surface is a long, narrow valley. The gradient points across the valley rather than down it, so training zig-zags and the learning rate is capped by the steepest direction. Rescaling makes the contours closer to circular, and the same optimizer converges in far fewer steps. why normalize inputs
    • Distance is meaningless without it. kNN, k-means, SVMs with an RBF kernel and PCA all compute distances or variances across features. A feature measured in dollars dominates one measured in ratios purely because its numbers are bigger โ€” it hijacks the metric.
    • Penalties are scale-sensitive. An L1/L2 penalty (see Regularization) charges by weight magnitude, and the weight a feature needs is inversely proportional to its scale. Without scaling, the penalty punishes small-unit features hardest, for no principled reason.
    • Saturating activations. Large inputs push sigmoid/tanh units into their flat regions where gradients vanish, so the layer stops learning.
  • [Where it does not matter]
    • Decision trees, random forests and XGBoost split on thresholds within a single feature at a time. Any monotonic rescaling produces the same splits, so scaling changes nothing but wasted time.
    • Ordinary least squares linear regression solved in closed form is also scale-invariant in its predictions โ€” but not in its conditioning, its coefficient interpretability, or the moment you add a penalty.

< The common transforms >โ€‹

Standardization (z-score) x' = (x โˆ’ ฮผ) / ฯƒ โ†’ mean 0, std 1, unbounded
Min-max normalization x' = (x โˆ’ min) / (max โˆ’ min) โ†’ squeezed into [0, 1]
Robust scaling x' = (x โˆ’ median) / IQR โ†’ outlier-resistant
Max-abs scaling x' = x / max(|x|) โ†’ [โˆ’1, 1], preserves zeros/sparsity
Unit-vector (L2) norm x' = x / โ€–xโ€–โ‚‚ โ†’ each *row* has length 1
  • The first four are per-column (fit one statistic per feature, across rows). Unit-vector normalization is per-row and answers a different question: it makes direction matter and magnitude not โ€” standard for TF-IDF vectors and embedding similarity.
  • Names are used loosely in the wild. "Normalization" often means min-max specifically, and often means scaling in general; when it matters, say which formula you mean.

Key pointsโ€‹

< The rule that breaks pipelines: fit on train only >โ€‹

  1. [Fit the scaler on the training split] compute ฮผ, ฯƒ, min, max from training rows only.
  2. [Apply those same numbers to validation, test and production] never re-fit. Re-fitting on the test set leaks its distribution into preprocessing and inflates your score โ€” the model has seen something it will not have at inference.
  3. [Inside cross-validation, scale inside each fold] scaling the whole dataset before k-fold leaks every fold's statistics into every other fold. Wrap the scaler and the model in a single pipeline object (sklearn.pipeline.Pipeline) so the framework refits per fold for you.
  4. [Ship the fitted scaler with the model] the ฮผ/ฯƒ vector is part of the artifact. A model deployed without its exact training-time scaler is silently wrong.

< Choosing a transform >โ€‹

TransformOutputUse whenAvoid when
StandardizationMean 0, std 1Default. Linear/logistic models, SVM, PCA, neural netsData is very heavy-tailed (ฮผ, ฯƒ are themselves distorted)
Min-max[0, 1]You need a bounded range โ€” image pixels, some NN inputs, distance metrics that assume a boxOutliers exist; a single extreme value crushes everything else toward 0
RobustMedian 0, IQR 1Outliers you want to keep but not be dominated byData is clean and roughly symmetric โ€” no benefit
Max-abs[โˆ’1, 1]Sparse matrices โ€” it never shifts, so zeros stay zerosDense data where centering would help
Unit-vectorRow norm 1Text/TF-IDF, embeddings, cosine similarityFeature magnitude carries real signal

< The awkward cases >โ€‹

  • Skewed distributions. Scaling moves and stretches a distribution; it does not reshape it. For heavy right skew (income, counts, durations) apply log1p, Box-Cox or Yeo-Johnson first, then standardize. Quantile transforms map to a uniform or normal shape directly.
  • Sparse data. Never mean-center a sparse matrix โ€” subtracting ฮผ makes every zero nonzero and can explode a 1 GB matrix into 100 GB. Use max-abs, or standardize with_mean=False.
  • One-hot / binary columns. Already on a comparable scale; scaling them is usually unnecessary and makes coefficients harder to read. Leave them, and scale only the continuous columns (ColumnTransformer).
  • Outliers. Clip or winsorize before scaling if the extremes are errors; use robust scaling if they are real.
  • The target. For regression, scaling y can help optimization but every prediction must be inverse-transformed back. Metrics computed on scaled targets are not comparable to anything.
  • Train/serve skew. Categorical encodings, missing-value imputation and scaling must happen identically in both paths. This is the single most common source of "the model performed worse in production".

< Normalization inside the network >โ€‹

Input scaling is preprocessing; these are layers that renormalize activations during the forward pass, and they solve a related but distinct problem โ€” keeping the distribution of each layer's inputs stable as the weights below it change.

LayerNormalizes overTypical home
Batch normEach feature, across the minibatchCNNs / vision. Batch-size dependent; train and inference behave differently
Layer normAll features, within one exampleTransformers, RNNs. Batch-size independent
RMSNormSame as layer norm, scale only (no mean subtraction)Modern LLMs โ€” cheaper, works as well in practice
  • Internal normalization reduces but does not remove the need to scale inputs: the first layer still receives raw features, and its gradients still see the raw geometry.
  • Vision convention: divide pixels by 255, then subtract per-channel mean and divide by per-channel std using the statistics of the pretraining dataset โ€” using the wrong published constants is a classic silent accuracy loss when fine-tuning.

< Mistakes to check for >โ€‹

MistakeSymptom
Fit scaler on the full dataset before splittingValidation score better than production, unexplained
Scaler refit at inference on one batchPredictions swing with batch composition
Forgot to inverse-transform predictionsRegression outputs near 0, "the model predicts nothing"
Scaled the one-hot columns and interpreted coefficientsCoefficient sizes no longer comparable
Min-max on data with an outlierAlmost all values collapse into a tiny sliver of [0, 1]
Centered a sparse matrixMemory blow-up, or a training job that never starts

Referenceโ€‹