Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

OneHotEncoder

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.

How it works

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.

Syntax

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')

Parameters

NameTypeDescription
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=NoneCategory-dropping strategy or explicit categories to drop.
sparse_outputbool, default=TrueWhether to return a sparse matrix; false returns a dense array.
dtypenumber type, default=np.float64Numeric data type of the encoded output.
handle_unknown{'error', 'ignore', 'infrequent_if_exist', 'warn'}, default='error'Policy for categories not seen during fitting.
min_frequencyint or float, default=NoneMinimum frequency for grouping infrequent categories.
max_categoriesint, default=NoneMaximum number of output categories per input feature.
feature_name_combiner"concat" or callable, default="concat"Callable or string strategy for constructing output feature names.

Returns

A fitted OneHotEncoder instance from fit; transform and fit_transform return a sparse matrix or dense ndarray depending on sparse_output.

Key points

  • Use the class to convert categorical columns into numeric one-hot features for estimators that require numeric input.
  • Each learned category becomes a binary output column, with the output representation selected by sparse_output.
  • Fitting normally discovers the category vocabulary independently for every input feature.
  • Pass explicit category lists when the vocabulary or category ordering must be controlled.
  • One-hot encoding is useful before models such as linear estimators and standard-kernel support vector machines.

Example: Encode categorical features

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.

When to use it

Model-ready categorical features

Preparing categorical columns for linear models or standard-kernel SVMs.

Controlled feature schema

Keeping a fixed category vocabulary and ordering across datasets.

Robust production preprocessing

Handling rare values or unseen inference-time categories safely.

Interview questions

QHow does OneHotEncoder represent categorical values?

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.

QHow are categories determined?

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.

QWhat do drop, min_frequency, and max_categories control?

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.

  • OrdinalEncoder
  • MultiLabelBinarizer
  • ColumnTransformer
  • get_dummies

Source: OneHotEncoder — scikit-learn 1.9.1 documentation. Checked 2026-10-15.