Modules / NLP Encoding / Label Encoding

Label Encoding

Convert categorical text into unique numerical labels. Each unique word is assigned a distinct integer ID.

Encoded Sequence

0 TOKENS
Sequence List Scroll View

Overview

Label encoding maps categories or tokens to integer IDs. These numbers identify values; their size does not express meaning, order, or distance. Language models use token IDs as indices into learned embeddings, while ordinary numeric models may misread arbitrary category IDs as measurements.

From words to IDs

This token-ID example is related to, but not identical to, encoding a categorical feature column. Language models often use token IDs as indices into learned embedding tables. A standard linear model reading those IDs as numbers would instead interpret their order and spacing as values.

Choose an encoding for the model

SituationCommon choiceWhat the model receives
Target class labelsInteger class IDsA class identifier, not a numeric feature
Ordered feature such as satisfaction levelOrdinal encodingA numeric order, if the order is real and useful
Unordered feature such as cityOne-hot encodingA separate indicator for each category
Many categories with a neural modelLearned embeddingsA trainable vector for each category or token

For a nominal feature, codes such as London=0, Manchester=1, and Leeds=2 invent a ranking. A linear model treats a one-step code change as a fixed numeric effect; a distance-based model treats some categories as farther apart than others. One-hot encoding avoids that false ordering, but can create many columns for high-cardinality features.

Some tree libraries support categorical features directly. Ordinary decision trees that receive numeric IDs still split by thresholds, so arbitrary coding can constrain which category groups are formed. Check the estimator’s categorical handling rather than assuming all trees treat IDs as labels.

Handle categories not seen in training

An encoder fitted on London, Manchester, and Leeds must have a policy for a new category such as Bristol. Fit one mapping on training data and apply it unchanged to validation, test, and production rows. Do not fit separate encoders on different splits.

Use an encoder with an explicit unknown-category behavior, group rare values into an “other” category when appropriate, or use an estimator that can handle unseen categories. For high-cardinality target encoding, compute encodings without using each row’s own target; use cross-fitting or another leakage-safe method.

Common mistakes with Label Encoding Process

  • Using arbitrary IDs as measurements: the numbers may create a fake order or distance.
  • Confusing target labels with input features: integer class IDs are valid targets but do not make good numeric features by default.
  • Ignoring new values: production data may contain categories absent from training.
  • Fitting target encodings before splitting: target information leaks into the feature.

Sources and next steps

Predict, then reveal

About to run: set View to its maximum (100). Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “From words to IDs”?

  2. What does this module say about “Choose an encoding for the model”?

  3. What does this module say about “Handle categories not seen in training”?

Cheat sheet

Label Encoding Process

Turn categories into integers, and see where label encoding is safe and where it invents an ordering your model will believe.

MACHINE LEARNING · vizlearn.in/machine_learning/label_encoding.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.