Local outlier factor (LOF) detects observations that are sparse relative to their neighbors. It estimates density from distances to nearby points: shorter distances indicate higher density. LOF then compares each observation’s density with densities in its neighborhood, rather than applying one density standard to the entire dataset. section 5.3
This local standard matters when ordinary groups have different densities. Purchase transactions of one type may occupy a narrower range of amounts, dates, or tax rates than another type. A transaction can therefore be unusual among comparable purchases without being unusual by a dataset-wide distance rule. section 5.3
Figure 5.3 Example motivating the LOF algorithm. Two points can reasonably be considered outliers: O1, and O2, with O1 an outlier of the C1 cluster and O2 an outlier of the C2 cluster. Using the normal distances between points in C1, O2 would be considered normal, and using the normal distances between points in C2, many points in C1 besides O1 would be outliers.
Two clusters have different densities. Each has a plausible outlier, but a distance standard taken from either cluster misclassifies points in the other. section 5.3
For tabular data, a practical workflow is:
- Encode categorical columns and scale numeric features before fitting LOF; its distance calculations make scaling important. section 5.1 section 5.3
- Choose the number of neighbors, k, and inspect how results change across plausible values. Its effects can be unintuitive, particularly in high dimensions. section 8.1.1
- Interpret flagged observations as candidates for investigation, not definitive anomalies. Compare LOF with other detectors when the kinds of outliers sought are uncertain. section 5.0 section 8.1.1
Fit a neighborhood-based detector
After encoding and scaling the features, `LocalOutlierFactor(n_neighbors=20).fit_predict(X_prepared)` returns labels: `-1` for flagged outliers and `1` for inliers. Treat 20 as a value to examine, not a universal setting; compare results for other neighbor counts. section 5.1 section 8.1.1
LOF’s local view contrasts with global distance-based detectors, which may focus on smaller clusters; local detectors may instead emphasize cluster edges. Neither perspective is always preferable. The choice depends on which observations matter and how the detectors behave on the data. section 8.1.1 section 5.8
