Data Science Scale

Feature scaling puts variables measured in different units onto a common footing so that distance-based and gradient-based algorithms treat every feature fairly.

Why Features Need a Common Scale

Real datasets mix wildly different units: a person's age might range from 18 to 90, while their income ranges from 20,000 to 500,000. If you feed these raw numbers into an algorithm that measures distance or computes gradients, the feature with the larger numeric range will dominate the calculation purely because of its scale, not because it is actually more informative.

  • Distance-based algorithms: K-Nearest Neighbors, K-means clustering, and Support Vector Machines all compare points using distances (usually Euclidean), so a feature with a bigger range silently outweighs the others.
  • Gradient-based algorithms: linear regression, logistic regression, and neural networks trained with gradient descent converge faster and more reliably when every feature varies over a similar range.
  • Regularized models (Ridge, Lasso): the penalty term is applied uniformly to coefficients, so unscaled features get penalized unevenly.

The Z-score Standardization Formula

The most common scaling technique is standardization, which converts every value to a z-score: z = (x - mean) / standard_deviation. After this transform, each feature has a mean of 0 and a standard deviation of 1, so no single feature can dominate just because its raw numbers happen to be larger.

Example

from sklearn.preprocessing import StandardScaler
import numpy as np

# Two features on very different scales: age (years) and income ($)
X = np.array([
    [25, 42000],
    [32, 58000],
    [47, 120000],
    [51, 95000],
])

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

print('Mean of each column after scaling:', X_scaled.mean(axis=0).round(2))
print('Std of each column after scaling:', X_scaled.std(axis=0).round(2))
print(X_scaled.round(2))
FeatureRaw RangeAfter StandardScaler
Age (years)25 to 51-1.24 to 1.42
Income ($)42,000 to 120,000-1.11 to 1.53

An alternative is min-max scaling, which squeezes every value into a fixed range, usually 0 to 1. This is useful when you know the algorithm needs bounded inputs (some neural network activations, for example) or when the data is not normally distributed and you want to preserve the shape of the original distribution.

Example

from sklearn.preprocessing import MinMaxScaler

# Reuses X from the previous example
scaler = MinMaxScaler()  # squeezes every column into [0, 1]
X_scaled = scaler.fit_transform(X)
print(X_scaled.round(2))
Note: Fit the scaler only on the training data with fit_transform, then use transform (not fit_transform) on the test data. This prevents information from the test set leaking into the scaling parameters, which would give you an overly optimistic evaluation.
Note: Not everything needs scaling. Tree-based models (decision trees, random forests, gradient boosting) split on raw thresholds and are unaffected by scale, and one-hot encoded categorical columns are already on a 0/1 scale, so scaling them again is unnecessary.
  • Standardization centers data at mean 0 with standard deviation 1 using the z-score formula.
  • Min-max scaling compresses data into a fixed range, typically [0, 1].
  • Scale before distance-based or gradient-based algorithms; skip it for tree-based models.
  • Always fit the scaler on training data only, then apply it to the test data.

Exercise: Data Science Scale

Why is feature scaling often applied before training certain models?