Label Encoding
Convert categorical text into unique numerical labels. Each unique word is assigned a distinct integer ID.
Convert categorical text into unique numerical labels. Each unique word is assigned a distinct integer ID.
Machine learning models are mathematical functions; they understand numbers, not text. Before we can train a model on categorical data (like city names, product types, or sentiments), we must convert that text into a numerical format. Label Encoding is one of the simplest techniques to achieve this. It works by assigning a unique integer to each unique category or "label" in your dataset.
The process is straightforward and consists of two main steps, which you can see in action by clicking the "Encode Corpus" button above.
First, the encoder scans the entire input text (the "corpus") to find all unique words (or "tokens"). It then sorts these unique words alphabetically to create a consistent vocabulary. This vocabulary acts as a dictionary or a look-up table.
Once the vocabulary is built, the encoder assigns a unique integer ID to each word, starting from 0. The first word in the sorted vocabulary gets ID 0, the second gets ID 1, and so on. The final output is a sequence of these integer IDs, representing the original text.
Use the interactive panel to understand the nuances of this technique.
While simple, Label Encoding has a major flaw. Most machine learning models will interpret the assigned integers as having an order or rank. For example, if you have categories 'Cat', 'Dog', and 'Fish', they might be encoded as 0, 1, and 2.
The model might incorrectly assume that 'Dog' (1) is "greater" than 'Cat' (0), or that the difference between 'Fish' (2) and 'Cat' (0) is twice the difference between 'Dog' (1) and 'Cat' (0). This is an arbitrary ordinal relationship that doesn't exist in the original data, and it can mislead your model.
Machine learning models are mathematical functions; they understand numbers, not text. Before we can train a model on categorical data (like city names, product types, or sentiments), we must convert that text into a numerical format. Label Encoding is one of the simplest techniques to achieve this. It works by assigning a unique integer to each unique category or "label" in your dataset.