devnotes.

Model-Based Outlier Detection

Univariate methods look at one column at a time. Some outliers only appear when several features are combined — for example, a person of age 5 with an income of 100,000. Machine learning based detectors handle these multivariate cases.

Isolation Forest

Randomly splits the data with trees. Outliers are easier to isolate, so they end up with shorter paths and get flagged.

from sklearn.ensemble import IsolationForest

X = df[["Age", "Fare"]].dropna()

iso = IsolationForest(contamination=0.02, random_state=42)
labels = iso.fit_predict(X)          # -1 = outlier, 1 = normal

print("Outliers found:", (labels == -1).sum())
print(X[labels == -1].head())

Local Outlier Factor (LOF)

Compares the local density around a point with the density around its neighbours. Points in much sparser regions are outliers.

from sklearn.neighbors import LocalOutlierFactor

lof = LocalOutlierFactor(n_neighbors=20, contamination=0.02)
labels_lof = lof.fit_predict(X)      # -1 = outlier
print("Outliers found:", (labels_lof == -1).sum())

DBSCAN (density-based clustering)

Points that do not belong to any dense cluster get the label -1 (noise).

from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler

Xs = StandardScaler().fit_transform(X)
db = DBSCAN(eps=0.5, min_samples=10).fit(Xs)
print("Noise points:", (db.labels_ == -1).sum())

Comparison

MethodIdeaStrengthLimitation
Isolation ForestOutliers are easy to isolateFast, works in high dimensionsNeeds a contamination estimate
LOFCompare local densitiesFinds local anomaliesSlower on large data
DBSCANPoints outside dense clustersNo need to specify the outlier shareSensitive to eps and min_samples
One-Class SVMLearn a boundary around normal dataGood for novelty detectionNeeds tuning and scaling

Tips

  • Scale the features first for distance/density-based methods (LOF, DBSCAN).
  • contamination is your guess of the outlier fraction; inspect the flagged rows manually.
  • Fit on training data only.
Model-Based Outlier Detection - Outliers | Data Preparation | DevNotes