Encoding In Machine Learning
Encoding is the process of converting text or categories (like
colors or labels) into numbers so that a computer can work with
them.
Why Encoding Is Important?
One Hot Encoding:
One-hot encoding is the most widely used categorical encoding
technique. It is suitable for nominal categorical variables, where the
categories have no relationship
Working-
1. For each category in a categorical column, a new binary column is
created
2. The binary column will have a value of 1 if the class is present, else it
will be zero
Python Code
import pandas as pd
# Sample dataset with a categorical column
data = {'Color': ['Red', 'Blue', 'Green', 'Red', 'Green']}
df = [Link](data)
# Perform one-hot encoding and convert to 0/1 integers
one_hot_encoded = pd.get_dummies(df, columns=['Color']).astype(int)
print(one_hot_encoded)
Pros: It preserves all information about the categories and doesn’t
introduce any ordinal relationship
Cons: It can lead to a high dimensionality problem when dealing with a
large number of classes
When to use: Ideally for categorical features with less than 10 categories
(max 50 categories can be considered)
Label Encoding-
Label encoding is suitable for categorical features. In this technique, each
category is assigned a unique integer label. Categories are assigned
integer values starting from 0.
Size Size_encoded
0 Small 2
1 Medium 1
2 Large 0
3 Medium 1
4 Small 2
Python Code—
from [Link] import LabelEncoder
# Sample dataset with a categorical column
data = {'Size': ['Small', 'Medium', 'Large', 'Medium', 'Small']}
df = [Link](data)
# Initialize the LabelEncoder
label_encoder = LabelEncoder()
# Fit and transform the 'Size' column
df['Size_encoded'] = label_encoder.fit_transform(df['Size'])
--- when working with CSV
import pandas as pd
from [Link] import LabelEncoder
# Load the dataset
data = pd.read_csv(‘your_data.csv’)
# Initialize the label encoder
label_encoder = LabelEncoder()
# Apply label encoding to a specific column
data[‘category_column’] = label_encoder.fit_transform(data[‘category_column’])
Pros: Works well for features with two categories
When to use: Categorical features with two categories
Ordinal Encoding –
Ordinal encoding is a way to convert categories (words) into numbers but in
a way that respects the order of the categories.
import pandas as pd
from [Link] import OrdinalEncoder
# Create sample DataFrame
df = [Link]({
'Product': ['A', 'B', 'C', 'D', 'E'],
'Quality': ['low', 'medium', 'high', 'medium', 'low']
})
# Define the custom order for the 'Quality' column
encoder = OrdinalEncoder(categories=[['low', 'medium', 'high']])
# Apply encoding only to the 'Quality' column
df['Quality_encoded'] = encoder.fit_transform(df[['Quality']])
# Display full DataFrame
print(df)
When to Use:
Use ordinal encoding when your categories have a rank or order, like:
Small, Medium, Large
Poor, Average, Good, Excellent
Beginner, Intermediate, Advanced
Don't Use It When:
If your data has no order (like red, blue, green), then use One-Hot Encoding instead.