tezvyn:

Label Encoding: Turning Categories into Numbers

AI-drafted, machine-checkedSource: scikit-learn.orgintermediate
Label Encoding: Turning Categories into Numbers

Label Encoding turns text categories into numbers, like assigning bib numbers to runners. It's essential for algorithms that need numerical input, but its biggest footgun is creating a fake order (e.g., 2 > 1) that can mislead linear models and neural…

WHY IT EXISTS: Machine learning models are fundamentally mathematical. They operate on numbers, not text labels like "cat" or "dog". Label encoding is a basic strategy to bridge this gap, translating categorical data into a format the algorithm can understand.

THE MENTAL MODEL: Think of Label Encoding as creating a simple, alphabetized dictionary for your data column. You find all the unique values ("cat", "dog", "fish"), sort them alphabetically ("cat", "dog", "fish"), and assign them integers starting from 0. So, "cat" becomes 0, "dog" becomes 1, and "fish" becomes 2. Every time you see "cat" in your original data, you replace it with 0.

HOW IT WORKS: The encoder scans a feature column to find all unique categories. It then sorts these unique categories alphabetically and assigns a unique integer to each one, starting from 0. For example, a column with ['S', 'M', 'L', 'S', 'XL'] would first find unique values ['L', 'M', 'S', 'XL']. After sorting, it assigns L=0, M=1, S=2, XL=3. The original column is then transformed into [2, 1, 0, 2, 3].

WHEN TO USE IT: Use Label Encoding in two main scenarios. First, for encoding your target variable (the 'y' value) in a classification problem; this is standard practice. Second, for encoding features when using tree-based models (like Decision Trees, Random Forests, Gradient Boosting). These models make splits based on values and are not typically confused by the artificial ordering.

WHEN NOT TO USE IT: Avoid using Label Encoding on input features for any model that is sensitive to the magnitude of values. This includes linear models (Linear/Logistic Regression), distance-based models (k-NN, SVMs), and neural networks. For these models, the encoder's output [0, 1, 2] implies that 2 is "more" than 1, and that the distance between 0 and 2 is twice the distance between 0 and 1. This artificial mathematical relationship can severely degrade model performance. Use One-Hot Encoding instead for these cases.

ONE CANONICAL EXAMPLE: In scikit-learn, LabelEncoder is used for the target variable. For features, OrdinalEncoder is the correct tool. If you have a feature for T-shirt size ['M', 'XL', 'S'] and you use an OrdinalEncoder, it might be converted to [1, 2, 0]. A linear regression model would incorrectly interpret size 'XL' as being twice as important as 'M', which is not a valid assumption. A tree-based model, however, could simply learn a rule like "if size > 1.5, then...".

Read the original → scikit-learn.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.