MediumFeature Engineering

Min-Max Scaling

Feature Engineering · Data Processing

Medium

Problem

Min-max scaling transforms every feature column independently into the range from 0 through 1. For a value in row i and column j, compute

x'_{ij} = \frac{x_{ij} - \min_j}{\max_j - \min_j}

The numerator subtracts the minimum value of column j, and the denominator is that column's maximum minus its minimum. If a column is constant, the denominator is zero; map every value in that column to 0.0. Return a floating-point matrix with the same shape as data.

Theory

Min-Max scaling, also called normalization, transforms features to a fixed range, typically [0, 1]. The minimum value becomes 0, the maximum becomes 1, and all other values are linearly scaled between them. This ensures all features have the same scale, which is critical for many machine learning algorithms.


Why Scale Features?

Distance-based algorithms: KNN, K-means, and SVM with RBF kernel compute distances between samples. A feature measured in thousands (e.g., salary) would dominate one measured in single digits (e.g., years of experience).

Gradient descent optimization: Neural networks and logistic regression converge faster when features are on similar scales. Large-scale features cause large gradients, leading to unstable training.

Regularization fairness: L1 and L2 penalties treat all features equally only when they are on the same scale.

Interpretability: After scaling, a value of 0.7 means "70% of the way from minimum to maximum" regardless of the original units.


The Min-Max Scaling Formula

For a feature column X, each value x is transformed to:

x' = \frac{x - x_{min}}{x_{max} - x_{min}}

Where:

Properties:


Scaling to Arbitrary Range

To scale to a range [a, b] instead of [0, 1]:

x' = a + (b - a) \cdot \frac{x - x_{min}}{x_{max} - x_{min}}

Common alternative ranges:


Column-wise Application

Min-Max scaling is applied independently to each feature (column):

Original data:

Compute min/max per column:

Scale each column:

Now both features range from 0 to 1.


Worked Example

Original feature values: [10, 20, 30, 40, 50]

Step 1 - Find min and max:

Step 2 - Apply formula to each value:

x'_1 = \frac{10 - 10}{40} = 0.0

x'_2 = \frac{20 - 10}{40} = 0.25

x'_3 = \frac{30 - 10}{40} = 0.5

x'_4 = \frac{40 - 10}{40} = 0.75

x'_5 = \frac{50 - 10}{40} = 1.0

Result: [0.0, 0.25, 0.5, 0.75, 1.0]


Handling Zero Range

When all values in a feature are identical (x_{max} = x_{min}), the denominator is zero:

x' = \frac{x - x_{min}}{0} = \text{undefined}

Solutions:


Train-Test Considerations

Critical rule: Compute x_{min} and x_{max} from training data only.

Why? Using test data statistics causes data leakage. The model would have indirect knowledge of test data distribution, leading to overly optimistic performance estimates.

Applying to test data:

x'_{test} = \frac{x_{test} - x_{min,train}}{x_{max,train} - x_{min,train}}

Consequence: Test values may fall outside [0, 1] if test data has values beyond the training range.


Inverse Transform

To convert scaled values back to original scale:

x = x' \cdot (x_{max} - x_{min}) + x_{min}

Use cases:


Outlier Sensitivity

Min-Max scaling is highly sensitive to outliers:

Example without outlier: [10, 20, 30, 40, 50]

Example with outlier: [10, 20, 30, 40, 1000]

A single outlier compressed all normal values near 0. Consider:


Min-Max vs Z-Score Standardization

Min-Max Scaling:

Z-Score Standardization:


Where Min-Max Scaling Shows Up

Examples

Example 1

Input
data = [[1, 10], [2, 20], [3, 30]]
Output
[[0.0, 0.0], [0.5, 0.5], [1.0, 1.0]]
Explanation
Each column is scaled using its own minimum and maximum.

Example 2

Input
data = [[0, 0], [10, 100], [20, 50]]
Output
[[0.0, 0.0], [0.5, 1.0], [1.0, 0.5]]

Hints

  1. Collect the minimum and maximum of one column before scaling its entries.
  2. Initialize an output matrix with the same row and column counts as data.

Requirements

Constraints

Starter Code

def min_max_scaling(data: list) -> list:
    """
    Returns each data column scaled to the range from 0 through 1.
    """
    # Write code here
    pass

Test Cases

CaseMatches
basicpublic
unevenpublic