scikit-learn 1 · Preprocessing and pipelines

Encode categories

Turn category labels into one-hot columns, ignoring categories unseen in training.

from sklearn.preprocessing import OneHotEncoder

encoder = OneHotEncoder(handle_unknown="ignore")
encoded = encoder.fit_transform([["red"], ["green"], ["red"]])

Official scikit-learn 1 documentation

More preprocessing and pipelines commands

All preprocessing and pipelines commands · scikit-learn 1 cheatsheet

Was this page helpful?