Interview Q&A / Machine Learning

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What is the difference between label encoding and one-hot encoding?

SHORT ANSWER

Label encoding maps each category to an integer, which falsely implies an order. One-hot encoding creates one 0/1 column per category with no implied order. Use one-hot for unordered categories in linear models; integer codes are fine for ordered ones or tree models.

Explain it simply

Cities are not ranked. If you code Delhi = 0, Mumbai = 1, Pune = 2, a linear model reads "Pune is twice Mumbai" and "Mumbai is the average of Delhi and Pune", which is nonsense. One-hot encoding avoids this by giving each city its own switch.

Example

text
City     label   is_Delhi  is_Mumbai  is_Pune
Delhi      0        1         0          0
Mumbai     1        0         1          0
Pune       2        0         0          1

Three cities become three columns. Since the three columns always add up to 1, one of them is redundant, so drop="first" leaves two columns and avoids the dummy variable trap in plain linear regression.

python
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(handle_unknown="ignore")

handle_unknown="ignore" stops the code failing when a new city appears at prediction time.

Ordered categories such as Low < Medium < High can use OrdinalEncoder with an explicit order, since the order is real.

What to say in the interview

"For nominal categories I use one-hot encoding so no false order is introduced. For ordinal ones I use ordinal encoding with a stated order. Tree models can cope with integer codes, but linear models, KNN and neural networks need one-hot. I handle unseen categories explicitly."

Common mistakes

  • Using LabelEncoder on input features; it is meant for targets.
  • Forgetting that one-hot on a column with thousands of values explodes the width.

#encoding#categorical#one-hot

Related questions

Practise more Machine Learning questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Machine Learning quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.