liveBook · Manning

Count encoding

As explained in Outlier Detection in Python

Count encoding transforms each categorical value into the frequency with which that value occurs in its column.

Count encoding replaces each categorical value with how often that value occurs in its column. Unlike ordinal encoding, which assigns arbitrary numeric labels, it preserves frequency: rare categories receive small values and common categories receive large values. section 4.9 section 9.4.3

Use count encoding with these practical considerations:

  • It converts each categorical column into one numeric column, making categorical data usable by detectors that require numeric inputs. section 4.9
  • Rare categories can become separated from common categories, which may help distance-based detectors identify unusual records. section 9.4.3
  • When several features are count-encoded, scale them so differing count ranges do not distort the detector. section 9.4.3
  • Treat the encoding as dataset- and objective-dependent; unsupervised settings make the best choice difficult to evaluate without labels or synthetic data. section 9.4

Department frequencies with LOF

For Department values occurring 1,000 times in Sales, 500 in Marketing, 100 in Engineering, 10 in HR, and 3 in Communications, map each category to its count. The 13 rare records then receive values 10 or 3, while common records receive 100, 500, or 1,000. Applying Local Outlier Factor to this single encoded feature identifies the rare records as outliers; the example predicts −1 for them and 1 for the rest. section 9.4.3

Listing 9.13 Identifying outliers in a single count-encoded column
import numpy as np
import pandas as pd
from sklearn.neighbors import LocalOutlierFactor
 
vals = np.array(['Sales']*1000 + ['Marketing']*500 + ['Engineering']*100 + 
                ['HR']*10 + ['Communications']*3)             #1
df = pd.DataFrame({"C1": vals})
vc = df['C1'].value_counts()                                  #2
map = {x:y for x,y in zip(vc.index, vc.values)}
df['Ordinal C1'] = df['C1'].map(map)
 
clf = LocalOutlierFactor(contamination=0.01)                  #3
df['LOF Score'] = clf.fit_predict(df[['Ordinal C1']])

Shows the pandas value-count mapping and a single-feature Local Outlier Factor workflow. section 9.4.3

How Outlier Detection in Python covers it

How deep it goes
Explains frequency mapping, rarity geometry, scaling, and a LOF demonstration, but does not establish one universally best encoding.
Examples
Uses Color mappings, department frequencies, pandas value counts, and a Local Outlier Factor implementation.
What it assumes
Assumes categorical columns and a numeric outlier detector, while defining the frequency mapping and its interpretation.
What you can do after
Apply it via pandas value counts and mapping, then assess encoded rarity with Local Outlier Factor.
How it connects
Connects categorical encoding with ordinal encoding, one-hot encoding, outlier detection, Local Outlier Factor, and feature scaling.

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.