Lesson 1 of 3 Feature Engineering

Scaling Features with Standardization and Normalization

This lesson explains how scaling puts features on comparable scales, since algorithms that compare values directly or update weights by gradients are dominated by features with larger ranges.

Learning outcomes

After completing this lesson, you will be able to:

  • Apply standardisation with the z-score formula.
  • Apply min-max normalisation.
  • Identify which algorithms need scaling and which do not.

Standardization

Standardization, also called z-score scaling, transforms each value with the formula (x - mean) / standard deviation, producing a feature with a mean of 0 and a standard deviation of 1. For example, ages with a mean of 40 and a standard deviation of 10 become -1 for age 30 and 2 for age 60.

Standardization keeps outliers as large values rather than squeezing them into a fixed range, and it suits algorithms that assume centred data or combine features in weighted sums, such as linear and logistic regression, support vector machines, principal component analysis (PCA) and neural networks.

Fill in the value

Compute the scaled value

Exam skills 1.2.4

Calculate standardised and normalised values by hand.

Log in to try this activity Interactive activities are free with an account.

Normalization

Min-max normalisation rescales values to a fixed range, usually 0 to 1, with (x - min) / (max - min). It preserves the shape of the distribution but is sensitive to outliers, because one extreme value compresses all others. It suits pixel values, neural network inputs and distance-based algorithms when there are no extreme outliers. The term normalisation is also used for scaling each row vector to unit length, as with normalised embeddings.

Robust scaling, which uses the median and interquartile range, is an option when outliers are present.

Which algorithms need scaling

Whether scaling matters depends on how an algorithm uses feature values. Distance-based algorithms, such as k-nearest neighbours and K-Means, compare raw distances, so a feature measured in thousands dominates one measured in units. Gradient-based models, such as linear models and neural networks, train faster and more reliably with scaled inputs, and PCA is distorted by features with large variances.

Tree-based models, such as XGBoost, random forests and LightGBM, split on thresholds and are unaffected by monotonic scaling. In every case, scaling parameters, such as the mean and standard deviation, must be fitted on the training data only and then applied unchanged to validation, test and production data; fitting them on all the data leaks information about the test set into training. The following table summarises the methods.

MethodFormulaNotes
Standardization(x - mean) / stdMean 0, std 1; good default for linear models and neural networks
Min-max normalisation(x - min) / (max - min)Range 0-1; sensitive to outliers
Robust scaling(x - median) / IQRResistant to outliers

The following diagram compares each feature's share of the distance between two customers before and after standardisation, when spend is measured in thousands and visits in tens.

How each feature contributes to the distance between customers A and B. On raw values, the spend difference of 180,000 overwhelms the visits difference of 50, so K-Means clusters only by spend. After standardisation with training-set statistics, the differences are 2.7 and 3.0 standard deviations, so both features shape the clusters.
The scenario is illustrative.
True or false

When scaling matters

Exam skills 1.2.4

Check where feature scaling changes a model's behaviour, without repeating the lesson's list.

Log in to try this activity Interactive activities are free with an account.
Worked example

K-means on customer data

A team clusters customers with K-Means on two features: annual spend (0 to 250,000) and number of visits (0 to 60). Every cluster ends up defined almost entirely by spend.

Customer   spend     visits   spend_z   visits_z
A          200,000   5         2.1      -1.2
B          20,000    55       -0.6       1.8
Euclidean distance before scaling: dominated by spend (180,000 vs 50)
After standardization: both features contribute comparably

Because K-Means uses Euclidean distance, a difference of 180,000 in spend overwhelms a difference of 50 visits. Standardizing both features with statistics computed on the training data lets visit frequency influence the clusters too. An XGBoost model on the same data would not have needed this step.

Check yourself

Which algorithms need scaled features, and which do not?

Show answer

Distance-based and gradient-based algorithms, such as k-means and neural networks, need them. Tree-based models do not.

Exam Tips

Remember for the exam

  • Distance and gradient-based algorithms need scaled features; tree models do not.
  • Standardization: mean 0, std 1; min-max: range 0-1.
  • Outliers: robust scaling or cap values before min-max.
  • Fit scalers on training data only.
  • Apply the same fitted scaler at inference time.