Categorical columns often contain labels such as city names, plans, or browser families that estimators cannot treat as ordered numbers. OneHotEncoder in scikit-learn turns each learned category into its own binary feature, which keeps category labels distinct before model fitting.
OneHotEncoder learns the category list during fit and reuses that fitted list during transform. Setting handle_unknown="ignore" keeps later rows with unseen labels from raising an error, and sparse_output=False makes a small smoke-test matrix readable as dense output.
Use the standalone encoder when the input table contains only categorical columns or when a notebook check needs visible feature names. Mixed numeric and categorical tables usually belong in a ColumnTransformer so categorical encoding and numeric preprocessing are fitted together without leaking information from validation data into training.
Steps to one-hot encode categorical features with scikit-learn:
- Create a scratch Python file with two categorical columns.
- one_hot_categories.py
from sklearn.preprocessing import OneHotEncoder columns = ["city", "plan"] train = [ ["Paris", "free"], ["Tokyo", "paid"], ["Paris", "paid"], ["London", "free"], ] validation = [["Berlin", "paid"]] encoder = OneHotEncoder(handle_unknown="ignore", sparse_output=False) encoded_train = encoder.fit_transform(train).astype(int) encoded_validation = encoder.transform(validation).astype(int) feature_names = encoder.get_feature_names_out(columns) print("Learned categories:") for name, categories in zip(columns, encoder.categories_): print(f"- {name}: {', '.join(categories)}") print() print("Encoded feature names:") print(", ".join(feature_names)) print() print("Encoded training rows:") print(encoded_train) print() print("Validation row with unseen city:") print(encoded_validation) print() print(f"Encoded shape: {encoded_train.shape[0]} rows x {encoded_train.shape[1]} columns")
- Run the encoding script.
$ python one_hot_categories.py Learned categories: - city: London, Paris, Tokyo - plan: free, paid Encoded feature names: city_London, city_Paris, city_Tokyo, plan_free, plan_paid Encoded training rows: [[0 1 0 1 0] [0 0 1 0 1] [0 1 0 0 1] [1 0 0 1 0]] Validation row with unseen city: [[0 0 0 0 1]] Encoded shape: 4 rows x 5 columns
- Confirm the validation row keeps the unseen city out of the learned city columns.
Validation row with unseen city: [[0 0 0 0 1]]
The first three output positions map to city_London, city_Paris, and city_Tokyo. The unseen Berlin value produces zeros in those positions because handle_unknown="ignore" is set.
- Remove the scratch file when it was only needed for the smoke test.
$ rm one_hot_categories.py
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.