Modules / NLP Encoding / One Hot

One Hot Encoding

Explore how text is transformed into sparse binary vectors for neural network processing.

Encoded Vectors

0 VECTORS
Vector List Scroll View

Overview

One-hot encoding represents each category with its own 0/1 feature. It is appropriate for nominal categories—values with no natural order—because the encoded values do not imply that one category is larger or closer than another.

A small example

For a colour feature with values red, green and blue, create three columns: is_red, is_green and is_blue. A green row becomes [0, 1, 0]. This avoids the misleading numeric order created by assigning red=0, green=1 and blue=2.

The visualiser applies the same representation to words in a text corpus. Each unique token acts like a category: it receives one vocabulary position, and each token’s vector has a 1 at that position. This is a small example of one-hot representation; production text models often use counts, weighted counts or learned embeddings instead.

Explore the vocabulary

  1. Click Encode Corpus and inspect Vocabulary and Vec Length. Each token maps to a vector whose length is the number of unique vocabulary entries.
  2. Add a word that is not in the current text and encode again. The vocabulary and vector length grow; repeating an existing word adds another token but does not add a new vocabulary column.

The trade-offs

One-hot features are easy to interpret and work well for low-cardinality categories. They can be sparse: each row has only a small number of ones in a wide matrix. Sparse storage helps, but a feature with thousands of distinct values can still increase memory and make estimation difficult, especially when many categories appear only a few times.

For a categorical feature with k possible values, a full encoding uses k columns. In a linear model with an intercept, one column is often dropped to avoid exact collinearity; the remaining coefficients are interpreted relative to that reference category. Whether to drop a category depends on the estimator and regularisation—do not apply it blindly to every model.

SituationCommon choice
A few unordered categoriesOne-hot encoding
Categories with a real orderOrdinal encoding, with the order stated
Very many categoriesNative categorical handling, feature hashing or carefully validated target encoding
Text with a large vocabularySparse count or TF–IDF features; embeddings for suitable neural models

Unseen categories at prediction time need an explicit policy. Scikit-learn’s OneHotEncoder documentation describes options such as handling unknown values and sparse output. Fit the encoder on training data only, then reuse it for validation and test data; a pipeline helps prevent leakage. See label encoding for the alternative that maps categories to integers.

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.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why not just number categories 1, 2, 3 and feed them in directly?

  2. A column has 50,000 distinct values. One-hot encoding it will:

  3. When does label encoding become the right choice?

Cheat sheet

One-Hot Encoding

Expand a categorical column into binary columns and see how one-hot encoding avoids implying an order that was never there.

MACHINE LEARNING · vizlearn.in/machine_learning/one_hot_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.