Model-ready categorical features
Preparing categorical columns for linear models or standard-kernel SVMs.
Scikit-Learn · Class
OneHotEncoder converts categorical features into binary one-hot columns. It learns or accepts category vocabularies and can control output sparsity, unknown values, dropped categories, and infrequent-category grouping.
OneHotEncoder learns categories from categorical columns and expands each category into a binary feature. It can omit a category, group infrequent values, tolerate unknown values, and return either a sparse matrix or a dense array.
class sklearn.preprocessing.OneHotEncoder(*, categories='auto', drop=None, sparse_output=True, dtype=<class 'numpy.float64'>, handle_unknown='error', min_frequency=None, max_categories=None, feature_name_combiner='concat')| Name | Type | Description |
|---|---|---|
categories | 'auto' or list of array-like, default='auto' | Category values for each feature, or 'auto' to infer them during fitting. |
drop | {'first', 'if_binary'} or array-like, default=None | Category-dropping strategy or explicit categories to drop. |
sparse_output | bool, default=True | Whether to return a sparse matrix; false returns a dense array. |
dtype | number type, default=np.float64 | Numeric data type of the encoded output. |
handle_unknown | {'error', 'ignore', 'infrequent_if_exist', 'warn'}, default='error' | Policy for categories not seen during fitting. |
min_frequency | int or float, default=None | Minimum frequency for grouping infrequent categories. |
max_categories | int, default=None | Maximum number of output categories per input feature. |
feature_name_combiner | "concat" or callable, default="concat" | Callable or string strategy for constructing output feature names. |
A fitted OneHotEncoder instance from fit; transform and fit_transform return a sparse matrix or dense ndarray depending on sparse_output.
from sklearn.preprocessing import OneHotEncoder
X = [["red", "S"], ["blue", "M"], ["red", "M"]]
encoder = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
encoded = encoder.fit_transform(X)
print(encoded)
print(encoder.get_feature_names_out(["color", "size"]))Fits on the observed categories, returns a dense binary array, and ignores unseen categories during later transforms.
Preparing categorical columns for linear models or standard-kernel SVMs.
Keeping a fixed category vocabulary and ordering across datasets.
Handling rare values or unseen inference-time categories safely.
It represents each category with a separate binary column, placing one in the column for the observed category and zero in the others. The result is sparse by default, or dense when sparse_output=False.
Follow-up: How would you handle categories at inference time that were absent during fitting? Use handle_unknown='ignore' so transformation succeeds and that feature's encoded block is all zeros.
By default, categories are inferred from unique values in each feature during fitting. You can instead provide categories explicitly as a list of array-like category collections.
Follow-up: When might explicit categories be useful? They can make the expected category set deterministic across training data or enforce a prescribed ordering.
The encoder supports drop=None, drop='first', and drop='if_binary', as well as an array specifying categories to drop. Dropping reduces redundant columns but can affect model interpretation and symmetry.
Follow-up: How can high-cardinality or rare categories be controlled? Use min_frequency to group infrequent categories and max_categories to limit the resulting categories.
OrdinalEncoderMultiLabelBinarizerColumnTransformerget_dummies