Feature Scaling

easy · features, normalization, standardization

Feature Scaling

Implement feature scaling methods used in data preprocessing.

Functions to implement

1. min_max_scale(X)

Scale features to [0, 1] range using min-max normalization.

  • Input: A 2D list (samples × features)
  • Output: Scaled data and parameters (mins, maxs)

2. min_max_transform(X, mins, maxs)

Apply min-max scaling using pre-computed parameters.

  • Input: Data, min values, max values
  • Output: Scaled data

3. standardize(X)

Standardize features to mean=0, std=1 (z-score).

  • Input: A 2D list (samples × features)
  • Output: Standardized data and parameters (means, stds)

4. standardize_transform(X, means, stds)

Apply standardization using pre-computed parameters.

  • Input: Data, mean values, std values
  • Output: Standardized data

Examples

X = [[1, 10], [2, 20], [3, 30]]

# Min-max scaling
X_scaled, mins, maxs = min_max_scale(X)
# X_scaled: [[0, 0], [0.5, 0.5], [1, 1]]

# Standardization
X_std, means, stds = standardize(X)
# means ≈ [2, 20], stds ≈ [0.816, 8.16]

Notes

  • Each feature is scaled independently
  • Handle edge cases (constant features with std=0)
  • You may use math.sqrt
Run tests to see results
No issues detected