Skip to content
Machine Learning

What is one hot encoding?

Encoding a categorical feature as one binary column per category: each row gets a 1 in the column for its own category and 0 in all the others, so every encoded vector is sparse with exactly one 1.

datasciencetrivia.com

Card 1 of 259. Answer: Encoding a categorical feature as one binary column per category: each row gets a 1 in the column for its own category and 0 in all the others, so every encoded vector is sparse with exactly one 1.

Created by santiviquez

About · Quizzes · History of AI · To suggest new questions or report an error send me a dm.

The longer answer

One-hot encoding represents a categorical feature with k possible values as k binary columns, one per category. Each row has a 1 in the column of its own category and 0 in all the others, so every encoded vector is sparse and contains exactly one 1.

Example: a payment_method feature with values card, cash and wallet becomes three columns, is_card, is_cash and is_wallet. A cash payment is [0, 1, 0] and a wallet payment is [0, 0, 1]. Every pair of categories ends up equally far apart, so the encoding implies no order, which is what a nominal feature needs.

Interviewers often ask about the dummy variable trap. In a linear model with an intercept, the k columns always sum to 1 and are perfectly collinear with it, so you keep k − 1 columns and the dropped category becomes the baseline. In production, plan for categories never seen in training: scikit-learn's OneHotEncoder with handle_unknown='ignore' encodes them as all zeros. High-cardinality features usually need other encodings.

All questions