liveBook · Manning

Label encoding

As explained in Outlier Detection in Python

Ordinal encoding is a categorical encoding technique that maps discrete categories to integer codes representing an order.

Label encoding, also called ordinal encoding, replaces each categorical value with an integer code. For example, Color values Red, Blue, and Green can become 0, 1, and 2. The codes are labels, not measurements: their numeric order and spacing are arbitrary. section 4.9 section 9.4.2

The technique is commonly used when categorical features must be made numeric for an outlier detector. It avoids the extra binary columns produced by one-hot encoding, but the resulting representation must match the detector’s assumptions. section 3.3.2 section 9.4.2

Interpret label-encoded values according to the downstream detector:

  • Use ordinal encoding cautiously with distance-based detectors: arbitrary codes can create misleading distances between categories. section 9.4.2
  • Use it more favorably with Isolation Forest, which can treat repeated category values as duplicate records and exploit category frequency. section 9.4.2
  • Treat encoded categories as bins rather than ordered numeric measurements in HBOS; rare ordinal values contribute frequency information, not meaningful order. section 6.2

Department frequencies with Isolation Forest

Suppose a Department column contains 1,000 Sales records, 500 Marketing records, and 50 Engineering records, encoded as 0, 1, and 2. Isolation trees initially isolate category values similarly, then retain duplicate counts when nodes stop splitting. Engineering records require fewer estimated isolation steps and are therefore more likely to receive high outlier scores than common Sales records. section 9.4.2

How Outlier Detection in Python covers it

How deep it goes
Explains integer mapping and detector-specific behavior, but stops short of prescribing one universally best encoding.
Examples
Uses Color mappings, Department frequencies, and categorical bins in HBOS to demonstrate representation and detector effects.
What it assumes
Assumes categorical columns and outlier detectors, while explaining from zero why categorical values require numeric preprocessing.
What you can do after
Apply ordinal encoding when preparing categorical features for compatible unsupervised outlier detectors.
How it connects
Connects categorical data, one-hot encoding, count encoding, Isolation Forest, distance metrics, and HBOS.

Also explained in

Compare how 4 books explain label encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.