Contents
Encoding Categorical Variables
Machine learning algorithms generally require numerical input features. Categorical columns such as department, product type, or city may therefore need to be converted into numerical representations.
The appropriate encoding method depends on whether the categories have a meaningful order and how the model handles input data.
One-Hot Encoding
One-hot encoding creates a separate binary column for each category. It is useful for nominal categories that have no natural ranking.
import pandas as pd
df = pd.DataFrame({
"Customer": ["A", "B", "C", "D"],
"City": ["Delhi", "Chennai", "Mumbai", "Delhi"]
})
encoded_df = pd.get_dummies(
df,
columns=["City"],
dtype=int
)
print(encoded_df)
The resulting columns indicate whether each record belongs to a particular city.
One-hot encoding avoids assigning an artificial numerical order to categories. However, datasets with thousands of unique categories can produce many columns, increasing memory and computational requirements.
Ordinal Encoding
Ordinal encoding represents categories with a meaningful order.
For example, education levels may follow a progression from school to undergraduate to postgraduate education.
import pandas as pd
df = pd.DataFrame({
"Education": [
"School",
"Undergraduate",
"Postgraduate",
"Undergraduate"
]
})
education_order = {
"School": 0,
"Undergraduate": 1,
"Postgraduate": 2
}
df["Education_Level"] = df["Education"].map(education_order)
print(df)
Ordinal encoding is appropriate when the ordering is meaningful. Do not apply it to unrelated categories such as cities or product names merely because they can be assigned numbers.
Handling Unseen Categories
A machine learning model may encounter a category during prediction that was not present during training. A robust preprocessing workflow should define how such values will be handled.
Scikit-learn's OneHotEncoder supports this situation:
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
train = pd.DataFrame({
"City": ["Delhi", "Mumbai", "Chennai", "Delhi"]
})
test = pd.DataFrame({
"City": ["Mumbai", "Pune"]
})
encoder = OneHotEncoder(handle_unknown="ignore")
train_encoded = encoder.fit_transform(train[["City"]])
test_encoded = encoder.transform(test[["City"]])
print(train_encoded.toarray())
print(test_encoded.toarray())
The encoder learns categories from the training data. The unseen category Pune does not cause an error because handle_unknown="ignore" is enabled.
For larger datasets, consider keeping the encoded results in sparse form rather than converting them to dense arrays.
Python for Data Science
Vishalini A








