liveBook · Manning

One-hot encoding

As explained in Outlier Detection in Python

One-hot encoding represents each category of a categorical feature as a separate binary indicator feature.

One-hot encoding replaces each categorical feature with one binary indicator column per unique value. A row receives 1 in the column matching its category and 0 in the others, while existing numeric features remain unchanged. section 4.9

Encoding categorical features

A Color column containing Red, Blue, and Green becomes Color_Red, Color_Blue, and Color_Green. For baseball positions, get_dummies() creates columns such as Position_Catcher and Position_Outfield; each row has exactly one 1 across the position columns. section 4.9

One-hot encoding is commonly useful for outlier detection when a categorical feature has low cardinality, and it works well with many detectors. However, high-cardinality features create many columns. This can give those features disproportionate influence in IsolationForest and overrepresent categorical differences in distance calculations. section 9.4.1

Encoding choice depends on the dataset, detector, and outliers being sought. Ordinal encoding may be preferable for IsolationForest, while one-hot encoding can make the contribution of individual category values easier to inspect with feature-importance methods. section 9.4 section 13.2.1

Table 9.12 Dataset after one-hot encoding (view table figure)

Row

Red

Blue

Green

White

Yellow

Big

Small

1

1

0

0

0

0

1

0

2

0

1

0

0

0

0

1

3

1

0

0

0

0

1

0

4

0

1

0

0

0

1

0

Inspect a compact table showing how color and size categories become binary columns, including the unchanged one-hot pattern for each row. section 9.4.1

How Outlier Detection in Python covers it

How deep it goes
Defines the transformation, demonstrates its columns and distances, and evaluates detector-specific influence without deriving the encoding mathematically.
Examples
Uses color, size, baseball position, staff department, account, and mixed categorical-numeric distance tables.
What it assumes
Assumes categorical columns and numeric detector requirements, while defining the binary transformation from basic examples.
What you can do after
Apply it via pandas get_dummies() to convert categorical columns into binary features for numeric detection workflows.
How it connects
Connects categorical features with ordinal encoding, count encoding, IsolationForest, distance metrics, and feature importance.

Also explained in

Compare how 6 books explain one-hot 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.