Feature Scaling: Putting Your Data on the Same Yardstick
Feature scaling puts all data on a common scale, preventing features with large values from dominating your model. It's vital for distance-based algorithms (k-NN) and gradient descent. The key footgun is fitting the scaler to your test set, which leaks data.
WHY IT EXISTS: Many machine learning algorithms work by calculating distances or gradients. If one feature has values from 0 to 1,000,000 and another has values from 0 to 10, the first feature will completely dominate any calculation. The algorithm will effectively ignore the second feature. Feature scaling solves this by bringing all features to a similar magnitude, ensuring they have a comparable influence.
THE MENTAL MODEL: Think of it as putting all your data on the same yardstick. If you're comparing a building's height in feet to a room's length in inches, you first convert them to a common unit to make a fair comparison. Feature scaling does this for your data, ensuring a feature like 'salary' (10,000-1,000,000) doesn't overpower 'years of experience' (0-50).
HOW IT WORKS: Scaling transforms feature values into a new range. Two common methods are: first, Normalization (or Min-Max Scaling), which squeezes values into a fixed range, typically 0 to 1. It's calculated as (value - min) / (max - min). Second, Standardization (or Z-score scaling), which transforms data to have a mean of 0 and a standard deviation of 1. It's calculated as (value - mean) / standard_deviation. Standardization is often preferred as it is less sensitive to outliers.
WHEN TO USE IT: Feature scaling is critical for algorithms that are sensitive to the magnitude of features. This includes distance-based algorithms like k-Nearest Neighbors (k-NN), Support Vector Machines (SVMs), and clustering algorithms. It is also essential for models that use gradient descent for optimization, such as linear regression, logistic regression, and neural networks.
WHEN NOT TO USE IT: Tree-based algorithms like Decision Trees, Random Forests, and Gradient Boosting are generally immune to the scale of features. These models make decisions by partitioning data based on individual feature thresholds, so the relative scale between features doesn't affect their performance. Scaling provides no benefit for these models.
ONE CANONICAL EXAMPLE: The biggest mistake is data leakage. You must learn the scaling parameters (like min/max or mean/std) ONLY from your training data. Then, apply that SAME learned transformation to your validation and test data. If you scale the whole dataset at once, information about the distribution of your test set "leaks" into the training process, giving you an unrealistically good performance score.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.