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
