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.
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")
$ 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
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.
$ rm one_hot_categories.py