liveBook · Manning

Local outlier factor

As explained in Outlier Detection in Python

Local outlier factor is a density-based outlier detection algorithm that compares an observation's local density with the local densities of neighboring observations.

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

How Outlier Detection in Python covers it

How deep it goes
Explains neighbor-distance density and local comparison, then addresses preprocessing and neighbor-count sensitivity without deriving LOF mathematically.
Examples
Uses purchase transactions, unequal-density clusters, and a scikit-learn example that varies the neighbor count.
What it assumes
Assumes basic Python and tabular-data handling while explaining why categorical encoding, scaling, and neighborhood comparisons matter.
What you can do after
Apply it via scikit-learn’s LocalOutlierFactor to prepared tabular data and assess flagged points across neighbor settings.
How it connects
Contrasts local outlier detection with global distance methods and relates LOF to nearest neighbors, density estimation, and Isolation Forest.

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.