Skip to content

← Normalization step 6 of 6

Easy End-to-End

Feature Normalization Pipeline

Feature normalization puts columns with different units or scales on more comparable numerical scales. This exercise offers two transformations, with separate statistics for every column of the input matrix.

Z-score standardization subtracts the column mean and divides by its population standard deviation plus epsilon. For a column [1, 3, 5], the mean is 3 and the standard deviation is sqrt(8 / 3). The result is approximately [-1.224745, 0, 1.224745].

Min-max scaling subtracts the column minimum and divides by its range plus epsilon. For [0, 5, 10], the result is approximately [0, 0.5, 1]. Epsilon slightly reduces the maximum from 1 in exact arithmetic, though floating-point rounding can hide that difference.

Your task

Implement normalize_features(x, method):

  • x is a floating-point matrix of shape (N, D), with positive dimensions. Rows are examples and columns are features.
  • method is exactly "zscore" or "minmax".
  • Return a floating-point tensor of the same shape. Compute statistics from the supplied matrix; no stored statistics or fitting step is required.

Use a fixed eps = 1e-7 and reduce over axis 0:

  • For "zscore", return (x - mean) / (std + eps), where std is the square root of the mean squared deviation. Divide variance by N, not N-1. Add epsilon to the standard deviation, outside its square root.
  • For "minmax", return (x - minimum) / (maximum - minimum + eps).

A constant column returns zeros with either method. In particular, a single-row input returns all zeros. Do not replace a zero denominator with 1: adding epsilon is part of the formula even for nonconstant columns.

For a small column [0, 2e-7], z-score standardization gives [-0.5, 0.5], and min-max scaling gives approximately [0, 0.666667]. This makes the epsilon convention visible.

In a model-training workflow, learn these statistics from training data and reuse them for validation or test data. This exercise only implements the transformation using statistics from its given input.