Hello! Welcome to the next lesson in our Data Preprocessing and Feature Engineering module.
In our last lesson, we cleaned up our dataset by handling missing values. Now, our data has no gaps, but we're still left with columns containing text, like 'New York' or 'Female'. Machine learning models are mathematical, and they don't understand text—they need numbers.
Today's lesson addresses this exact problem. Your goal is to learn how to encode categorical variables using one-hot encoding and label encoding, and to explain the trade-offs between them. This is a critical step in preparing data for almost any machine learning model. By the end of this lesson, you'll be able to confidently choose the right encoding strategy based on your data and your model's needs.
1. Understanding Categorical Variables and Encoding Strategies
First, let's establish the different types of categorical data we'll encounter. This distinction is the most important factor in choosing an encoding method.
- Nominal Variables: These are categories without any intrinsic order. For example,
country('USA', 'France', 'Japan') orcolor('Red', 'Blue'). - Ordinal Variables: These are categories that have a meaningful order. For example,
education level('High School', 'Bachelors', 'Masters') orcustomer satisfaction('Low', 'Medium', 'High').
The goal of encoding is to convert these text labels into numbers. The two primary techniques we will cover are Label Encoding (or more appropriately, Ordinal Encoding) and One-Hot Encoding.
To get a clear initial understanding of these concepts, let's watch a short segment of a video.
Machine Learning Tutorial Python - 6: Dummy Variables & One Hot Encoding
The video 'Machine Learning Tutorial Python - 6: Dummy Variables & One Hot Encoding' by codebasics provides an excellent introduction to why we need to encode categorical variables and the difference between nominal and ordinal data.
Watch the video from the beginning until 02:52. Focus on why simply assigning numbers (like 1, 2, 3) to cities can be a problem and the distinction between nominal and ordinal variables.
As the video explained, simply assigning sequential numbers to a nominal variable like 'township' can mislead the model into thinking there's an order or a mathematical relationship between them (e.g., town_A + town_B = town_C), which is incorrect.
This brings us to our two main encoding strategies. This image provides a perfect visual summary of how they transform the data differently.

Now, let's clarify a common point of confusion: the difference between LabelEncoder and OrdinalEncoder in scikit-learn.
Encode categorical features using OneHotEncoder or OrdinalEncoder
The video 'Encode categorical features using OneHotEncoder or OrdinalEncoder' by Data School gives a very clear and concise explanation of the modern, correct way to think about these encoders.
Watch the beginning (00:00 - 00:17) and the section from 04:02 to 05:04. Pay close attention to the distinction made between OrdinalEncoder (for features) and LabelEncoder (for the target variable/labels).
The key takeaway is:
OrdinalEncoderis designed for encoding features (X).LabelEncoderis designed for encoding the target variable (y).
Although you might see older code or tutorials using LabelEncoder on features, it's best practice to use OrdinalEncoder for this task. We'll stick to this convention.
2. Ordinal Encoding: For Categories with Order
When your data is ordinal, like ['small', 'medium', 'large'], you want to convert it to numbers in a way that preserves this order, such as [0, 1, 2]. This allows the model to learn relationships like "as size increases, price increases." This is where OrdinalEncoder is the perfect tool.
How it Works and When to Use It
You use OrdinalEncoder when the categorical feature has a clear, inherent order. By encoding this order into a single numerical feature, you provide a strong signal to your model without adding extra complexity.
Let's read about its implementation and the importance of specifying the order.
Encoding of categorical variables — Scikit-learn course
The scikit-learn MOOC page, 'Encoding of categorical variables', provides an excellent, code-driven explanation of OrdinalEncoder.
Read the section titled 'Encoding ordinal categories'. Focus on how OrdinalEncoder works by default (alphabetical order) and how you can pass a specific order using the categories argument. This second point is crucial for correct implementation.
Here's a practical Python example where we correctly specify the order for a size feature.
import pandas as pd
from sklearn.preprocessing import OrdinalEncoder
# Sample data with an ordinal feature
df = pd.DataFrame({
'item': ['shirt', 'pants', 'shirt', 'hat'],
'size': ['M', 'L', 'S', 'L']
})
# Define the correct order of categories
size_order = ['S', 'M', 'L']
# Create the encoder with the specified order
# The list of lists corresponds to the columns you are encoding
ordinal_encoder = OrdinalEncoder(categories=[size_order])
# Apply the encoder
df['size_encoded'] = ordinal_encoder.fit_transform(df[['size']])
print(df)
# item size size_encoded
# 0 shirt M 1.0
# 1 pants L 2.0
# 2 shirt S 0.0
# 3 hat L 2.0
Notice that 'S' is mapped to 0, 'M' to 1, and 'L' to 2, exactly as we intended. If we had let the encoder decide, it would have used alphabetical order ('L', 'M', 'S'), which is incorrect.
3. One-Hot Encoding: For Categories Without Order
When your data is nominal (e.g., city names, car brands), there is no order to preserve. Using ordinal encoding would introduce a false ranking. The solution is One-Hot Encoding.
How it Works and When to Use It
One-Hot Encoding transforms each category into a new column and uses a 1 or 0 to indicate whether the category is present for a given row. This avoids the ordinality problem entirely.
There are two common ways to implement this in Python: pandas.get_dummies and scikit-learn's OneHotEncoder.
One Hot Encoding vs Label Encoding in Machine Learning
The article 'One Hot Encoding vs Label Encoding in Machine Learning' from Analytics Vidhya provides clear examples for both methods.
Read the sections 'What is One Hot Encoding?', 'Implementing One-Hot Encoding in Python using Scikit-Learn', and 'Implementing One-Hot Encoding using Pandas'. Note how pd.get_dummies is a bit simpler, while the scikit-learn version is more verbose but designed to fit into a modeling pipeline.
While pd.get_dummies is great for quick analysis, sklearn.OneHotEncoder is preferred for building machine learning models because it can be included in a preprocessing pipeline, which helps prevent data leakage (a concept we touched on in the last lesson).
Here is a consolidated code example:
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
# Sample data with a nominal feature
df = pd.DataFrame({
'product_id': [101, 102, 103, 104],
'color': ['red', 'blue', 'green', 'blue']
})
# --- Method 1: pandas.get_dummies() ---
df_dummies = pd.get_dummies(df, columns=['color'], prefix='color', dtype=int)
print("--- Using pandas.get_dummies() ---")
print(df_dummies)
# --- Method 2: sklearn.OneHotEncoder ---
# Create an instance of the encoder
ohe = OneHotEncoder(sparse_output=False, handle_unknown='ignore') # sparse_output=False returns a numpy array
# Fit and transform the 'color' column
encoded_features = ohe.fit_transform(df[['color']])
# Create a new DataFrame with the encoded features
encoded_df = pd.DataFrame(encoded_features, columns=ohe.get_feature_names_out(['color']))
# Concatenate with the original DataFrame (and drop the original column)
df_ohe = pd.concat([df.drop('color', axis=1), encoded_df], axis=1)
print("\n--- Using sklearn.OneHotEncoder ---")
print(df_ohe)
Notice I included handle_unknown='ignore'. This is a very useful parameter that tells the encoder to simply create a row of all zeros if it encounters a category in new data that it didn't see during training. This prevents errors when you deploy your model.
4. Trade-offs and Making the Right Choice
You now know how to implement both methods. The most important skill is knowing when to use each one. This involves considering the trade-offs.
The Dummy Variable Trap
One key trade-off with one-hot encoding is the Dummy Variable Trap. When you one-hot encode a feature with k categories, you create k new columns. These columns are perfectly multicollinear, meaning one can be predicted from the others (e.g., if a person is not from 'USA' and not from 'Canada', they must be from 'Mexico' if those are the only three options). This can cause issues for some models, like linear regression.
The common solution is to drop one of the one-hot encoded columns.
Machine Learning Tutorial Python - 6: Dummy Variables & One Hot Encoding
Let's return to the codebasics video, which has a great practical explanation of the dummy variable trap and how to handle it.
Watch from 02:52 to 07:43. Focus on how new 'dummy' columns are created and the explanation for why one of them should be dropped to avoid the 'dummy variable trap'. Note that scikit-learn's LinearRegression can handle this automatically, but it's good practice to do it explicitly.
You can achieve this in pd.get_dummies with the drop_first=True argument, or in OneHotEncoder with drop='first'.
Summary of Trade-offs
Let's crystallize the decision-making process.
One Hot Encoding vs Label Encoding in Machine Learning
The Analytics Vidhya article has an excellent summary table that lays out the core differences and trade-offs very clearly.
Read the sections 'When to Use a Label Encoding vs. One Hot Encoding' and the final comparison table under 'One-Hot Encoding vs Label Encoding'. This will serve as a great cheat sheet.
Here is a summary of the key decision points:
| Consideration | Use Ordinal Encoding | Use One-Hot Encoding |
|---|---|---|
| Data Type | Feature is ordinal (e.g., low, medium, high). |
Feature is nominal (e.g., red, green, blue). |
| Dimensionality | Keeps the number of features the same. Good for features with many categories (high cardinality). | Creates new features. Can lead to very high dimensionality if the feature has many unique categories. |
| Model Type | Generally safe for tree-based models (like Decision Trees, Random Forests) as they can split the feature anywhere. | The standard choice for linear models (like Linear/Logistic Regression) to avoid imposing a false order. |
| Information | Preserves the order, which can be a valuable signal. | Loses any sense of order but avoids creating a false one. |
Conclusion
Excellent work! You've just mastered a fundamental data preprocessing skill. Being able to correctly convert categorical data into a numerical format is essential for building effective machine learning models.
Key Takeaways:
- The choice between encoding methods depends primarily on whether the data is ordinal (has order) or nominal (no order).
- Use
OrdinalEncoderfor ordinal data, making sure to specify the correct order to preserve its meaning. - Use
OneHotEncoderfor nominal data to avoid creating a false sense of order. This is the safest default for most non-ordinal data. - Be mindful of the trade-offs:
- Ordinal Encoding is simple but can mislead a model if used on nominal data.
- One-Hot Encoding is robust but can increase dimensionality significantly (the "curse of dimensionality") and requires handling the dummy variable trap for certain models.
Preview of the next lesson:
Our dataset is now fully numerical! However, we might have features on very different scales (e.g., an age column from 18-80 and a salary column from 30,000-200,000). Many algorithms perform better when features are on a similar scale. In our next lesson, we will tackle this by learning to apply feature scaling techniques like standardization and normalization.