Welcome back. In our previous lesson, we established robust strategies for handling missing data, a critical first step in cleaning any real-world dataset. We've seen how the choice of imputation method involves a trade-off between simplicity, computational cost, and statistical validity. Now, we'll address another fundamental preprocessing task: preparing categorical data for machine learning models.
This lesson directly addresses the learning outcome of encoding categorical variables using one-hot, label, and target encoding. Machine learning algorithms operate on numbers, not text labels like "New York" or "credit card." Your ability to correctly translate these categories into a numerical format—and articulate the pros and cons of each translation method—is a core skill tested in any ML interview. We will explore the key techniques, focusing on the trade-offs that an experienced engineer and architect would consider, such as dimensionality, computational cost, and the risk of data leakage.
The Fundamental Divide: Ordinal vs. Nominal Data
Before choosing an encoding method, you must first understand the nature of your categorical variable. This is the most critical distinction. The video below from Emma Ding provides a clear, interview-focused explanation of the two types of categorical data.
Handling Categorical Data in Machine Learning Easy Explanation for Data Science Interviews
This video clearly defines the two fundamental types of categorical data, which dictates the appropriate encoding strategy.
Watch the initial segment that explains ordinal vs. nominal features.
To summarize:
- Ordinal Data: The categories have a natural, inherent order. For example,
['Low', 'Medium', 'High']or['Bronze', 'Silver', 'Gold']. The distance between ranks isn't necessarily uniform, but the order is meaningful. - Nominal Data: The categories have no intrinsic order. For example,
['Red', 'Green', 'Blue']or a list of city names.Greenis not "greater than"Red.
This distinction is paramount because choosing the wrong encoder can introduce false information that misleads your model.
Strategy 1: Label Encoding for Ordinal Data
When your data is ordinal, the simplest and most effective approach is Label Encoding (also called Ordinal Encoding). This method assigns a unique integer to each category, preserving the inherent rank.
One-Hot Encoding vs Label Encoding vs Target Encoding ... - Medium
This article provides a concise, practical overview of Label Encoding, perfect for understanding its core function and appropriate use cases.
Read the section on Label Encoding. Pay close attention to the "When to Use" and "Avoid" points.
Pros:
- Compact: It doesn't increase the dimensionality of your data; one column remains one column.
- Preserves Order: It correctly informs the model about the rank of the categories (e.g.,
High (2) > Medium (1) > Low (0)).
Cons:
- Danger on Nominal Data: If used on nominal data (like colors), it creates an artificial and misleading order. A linear model or SVM would incorrectly assume
Green (2)is twice as "valuable" asBlue (1), which is meaningless. While tree-based models can sometimes handle this by finding optimal split points, it's generally poor practice and can harm the performance of other model types.
Strategy 2: One-Hot Encoding for Nominal Data
For nominal data, the standard and safest approach is One-Hot Encoding. This technique avoids creating a false order by transforming the single categorical column into multiple new binary (0 or 1) columns, one for each unique category.

One-Hot Encoding vs Label Encoding vs Target Encoding ... - Medium
Let's return to the same article for an architect-level view of One-Hot Encoding and its implications.
Read the section on One-Hot Encoding. The "Architect Note" is particularly relevant for your interview preparation, as it frames the choice in terms of system performance.
Pros:
- No False Order: It treats all categories as independent, which is correct for nominal data. This is crucial for linear models, neural networks, and distance-based algorithms.
- Interpretability: The resulting model coefficients for each new column can be directly interpreted as the effect of that specific category.
Cons:
- The Curse of Dimensionality: If a feature has many unique categories (high cardinality), one-hot encoding will create an equal number of new columns. A feature like
zip_codecould add thousands of columns, dramatically increasing memory usage, training time, and model complexity. - Multicollinearity: By definition, the one-hot encoded columns are perfectly correlated (e.g., if you know
is_Redis 0 andis_Greenis 0, thenis_Bluemust be 1). To address this, it's common to drop one of the new columns (a technique called dummy encoding), though many modern libraries handle this internally.
Strategy 3: Target Encoding for High-Cardinality Data
What do you do when one-hot encoding is computationally infeasible due to high cardinality? This is a common problem in fintech with features like user_id, merchant_id, or transaction_category. The solution is Target Encoding.
Target encoding is a powerful technique that replaces each category with a summary statistic of the target variable. For a classification problem, this is typically the probability of the positive class. For regression, it's the mean of the target.
Encoding Categorical Variables: One-Hot vs. Label ...
This article gives a clear explanation of what Target Encoding is and how it works, using a simple house price example.
Read the section on Target Encoding. Focus on understanding the core concept and the significant risk it introduces: target leakage.
The Pitfall: Target Leakage and Overfitting
The great power of target encoding comes with a great risk: data leakage. If you calculate the target mean for a category using the entire dataset and then use that value as a feature, you are allowing information about the target to "leak" into your training data. A row's feature value will be influenced by its own target value, leading to an artificially inflated performance on the training set and poor generalization to new data.
The StatQuest video below explains this concept and, more importantly, the standard solution.
One-Hot, Label, Target and K-Fold Target Encoding, Clearly Explained!!!
This StatQuest video masterfully explains the intuition behind target encoding, the problem of data leakage, and the robust solution using k-fold cross-validation.
First, watch the segment explaining the basic idea of target encoding. Then, jump to the crucial explanation of data leakage and k-fold target encoding. This is a key interview topic that demonstrates a sophisticated understanding of the method.
The solution, K-Fold Target Encoding, prevents leakage by ensuring that the encoding for any given data point is calculated using target values from other data points, never its own.

Pros:
- Handles High Cardinality: It converts a feature with thousands of categories into a single, highly informative numerical feature.
- Captures Predictive Power: It directly encodes the relationship between the category and the target, often providing a strong predictive signal.
Cons:
- Prone to Overfitting: Without proper controls like k-fold validation and smoothing, it can easily overfit by memorizing the target variable.
- Complex to Implement Correctly: Requires careful cross-validation logic to avoid data leakage.
Making the Choice: A Decision Framework
For your interview, you should be able to present a clear, structured framework for choosing an encoding method.
One-Hot Encoding vs Label Encoding vs Target Encoding ... - Medium
This section of the Medium article provides an excellent, concise decision guide based on cardinality.
Read the decision guide. This is a perfect summary to structure your interview answer.
Here is a summary of the thought process you should articulate:
- Analyze the Feature: Is the data ordinal or nominal?
- Check Cardinality: How many unique categories are there?
- Select the Method:
- Ordinal Data: Use Label (Ordinal) Encoding.
- Nominal Data, Low Cardinality (< 20): Use One-Hot Encoding. It's safe and interpretable.
- Nominal Data, High Cardinality (> 50): Use Target Encoding. Crucially, mention that you would implement it using a k-fold strategy to prevent data leakage and overfitting. You could also mention Frequency Encoding (replacing a category with its count/frequency) as a simpler, faster alternative if the popularity of a category is itself a useful signal.
Conclusion
You've now covered the three most important categorical encoding techniques. This isn't just a mechanical preprocessing step; it's what the article aptly calls "signal engineering." Your choice directly impacts model performance, computational efficiency, and the risk of overfitting. In a fintech context, correctly handling high-cardinality features like customer or merchant IDs with a robust method like K-Fold Target Encoding can be the difference between a weak model and a highly predictive one.
Key Takeaways:
- Ordinal vs. Nominal is Key: This initial distinction dictates your entire strategy.
- Label Encoding: For ordinal data only. Compact but creates a false order for nominal data.
- One-Hot Encoding: The standard for nominal data with low cardinality. Avoids false order but explodes dimensionality.
- Target Encoding: The go-to for high-cardinality nominal data. Powerful but requires careful implementation (k-fold validation) to mitigate the serious risk of data leakage.
- Articulate the Trade-offs: In an interview, your ability to discuss the pros and cons of each method in the context of dimensionality, model type, and data properties is what will set you apart.
In our next lesson, we will continue our journey through feature preprocessing by addressing another common data issue: outliers. We will learn statistical methods to detect them and discuss strategies for treating them.