Skip to content

Feature Scaling (MinMax vs Standard)

Scaling is mainly important for ML algorithms that use distances or gradients:

  • k-NN
  • k-means
  • SVM
  • linear/logistic regression (often)
  • neural networks

Tree-based models (like Random Forest) usually don’t need scaling.

Maps values to a fixed range (usually 0 to 1):

  • x_scaled = (x - min) / (max - min)

Centers and scales to mean=0 and std=1:

  • x_scaled = (x - mean) / std
MinMaxScaler vs StandardScaler
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler
 
x = np.array([[10], [20], [30], [100]])
 
mm = MinMaxScaler()
ss = StandardScaler()
 
print("MinMax:")
print(mm.fit_transform(x))
 
print("Standard:")
print(ss.fit_transform(x))

When doing ML:

  • Fit scaler on training data
  • Transform both train and test using that scaler

This avoids data leakage.

Here’s how features move from raw, mismatched ranges to a common scale a model can use.

diagram Feature scaling pipeline mermaid
How raw features are scaled using a scaler fit on training data, then applied to test data.
  • If data has strong outliers: StandardScaler can be influenced; consider RobustScaler.
  • If you need bounded values (0–1): use MinMaxScaler.

Scaling doesn’t change the shape of your data — it changes the ruler. The same four points below get remapped onto a 0–1 range, and again onto a mean-0, std-1 range.

sketch The same values on three different scales p5.js
Raw values, min-max scaled values, and standardized values shown on their own number lines.

Exercise 1 – Scale to [0, 1] with MinMaxScaler

Section titled “Exercise 1 – Scale to [0, 1] with MinMaxScaler”

Exercise 2 – Standardize with StandardScaler

Section titled “Exercise 2 – Standardize with StandardScaler”

Exercise 3 – Apply the manual z-score formula

Section titled “Exercise 3 – Apply the manual z-score formula”

With features on the same scale, turn categorical columns into numbers too — continue to One-Hot Encoding.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading