Definition
The local outlier factor (LOF) of a point is the average density of its nearest neighbours divided by its own density. A score near 1 means the point is about as crowded as its neighbours, so it is normal. A score well above 1 means it sits in a much emptier spot than they do, so it is an outlier relative to its own neighbourhood.
At a glance
- Task
- Unsupervised anomaly (outlier) detection; novelty detection on new points
- Input → output
- points → a score , with normal and outlying
- Learns
- nothing parametric: it stores the data, each point's k-distance and its local density
- Built from
- k-nearest-neighbour distances (the same neighbourhoods as k-NN and DBSCAN)
- Cost
- one k-NN query per point: brute force, roughly with a tree index in low dimensions
- Hyperparameters
- (
n_neighbors), the distance metric, and the threshold orcontamination - Assumes
- that distances mean something, so features must be on comparable scales
The idea in plain words
Picture a city map with one very crowded neighbourhood and one spread-out suburb. A house 300 m from its nearest neighbour is nothing special in the suburb, where every house is that far apart. The same house next to the crowded block is strange, because everything around it is packed together. A single global rule such as "flag anything more than 250 m from its neighbours" gets both cases wrong. It flags every suburban house and misses the odd one beside the crowded block.
LOF asks a local question instead: is this point less crowded than the points around it? It estimates a density for every point from its nearest neighbours, then compares each point's density with its neighbours' densities. Because the comparison is a ratio, the overall scale does not matter. A sparse cluster whose members are all equally sparse scores about 1 everywhere.
Two kinds of outlier
- Global outlier
- far from all the data. Almost any method finds it: distance to the nearest neighbours, z-scores, isolation forest.
- Local outlier
- not far in absolute terms, but much sparser than its own neighbourhood, for example a point just outside a tight cluster. Global distance rules miss it when another region of the data is loose. This is the case LOF was designed for.
The maths, one quantity at a time
Fix and a distance (usually Euclidean). Every quantity below is built from the previous one.
1. k-distance. Sort the other points by distance from . The closest form the neighbourhood , and the distance to the -th of them is the radius of the smallest circle around that holds neighbours.
- the -th nearest neighbour of (not counting itself)
- small in dense regions, large in sparse ones
- the k-neighbourhood of . It has exactly members unless there are ties at the boundary
2. Reachability distance. Distances between points that are very close together are noisy, since a tiny shift changes them a lot. LOF smooths them: the distance from to a neighbour is never allowed to be smaller than 's own k-distance.
- the true distance, used when lies outside 's k-circle
- used when lies inside 's k-circle: all such points look equally close to
3. Local reachability density. Average the reach-distances from to its neighbours and take the reciprocal. Short reaches mean high density.
- local reachability density, in points per unit distance (higher = more crowded)
- number of neighbours, normally
4. Local outlier factor. Compare 's density with the densities of its neighbours.
- p is as dense as its neighbours: an inlier, whether the region is tight or loose
- p is denser than its neighbours, for example near the core of a cluster
- p is sparser than its neighbours by roughly that factor: an outlier
LOF has no fixed cut-off. A value of 1.2 can be normal in noisy data, and 2 is a strong signal. scikit-learn's default (contamination="auto") flags points with , the threshold used throughout this article. Often the most useful output is simply the ranking of points by LOF.
Worked example: twelve points, k = 3
Two clusters of very different density: a tight cluster A–E around and a loose cluster F–J about 2 units apart. Then two suspects: P at sits about 1.5 units from the tight cluster, and Z at is far from everything. All numbers below were checked against scikit-learn's LocalOutlierFactor(n_neighbors=3) and agree to every printed digit (ratios use unrounded values).
| Point | (x, y) | 3 nearest neighbours (distance) | k-dist |
|---|---|---|---|
| A | (1.0, 1.0) | B 0.632 · E 0.721 · C 0.825 | 0.825 |
| B | (1.6, 1.2) | A 0.632 · C 0.721 · D 0.762 | 0.762 |
| C | (1.2, 1.8) | E 0.632 · D 0.707 · B 0.721 | 0.721 |
| D | (1.9, 1.9) | C 0.707 · B 0.762 · A 1.273 | 1.273 |
| E | (0.6, 1.6) | C 0.632 · A 0.721 · B 1.077 | 1.077 |
| F | (6.0, 1.0) | J 2.263 · G 2.550 · P 2.668 | 2.668 |
| G | (8.5, 1.5) | J 1.421 · F 2.550 · I 2.746 | 2.746 |
| H | (6.5, 4.0) | J 1.780 · I 2.508 · Z 2.828 | 2.828 |
| I | (9.0, 4.2) | J 2.126 · H 2.508 · G 2.746 | 2.746 |
| J | (7.6, 2.6) | G 1.421 · H 1.780 · I 2.126 | 2.126 |
| P | (3.4, 1.6) | D 1.530 · B 1.844 · C 2.209 | 2.209 |
| Z | (4.5, 6.0) | H 2.828 · P 4.535 · J 4.601 | 4.601 |
| Point | reach-dist to the 3 neighbours | mean | lrd |
|---|---|---|---|
| A | 0.762 · 1.077 · 0.825 | 0.888 | 1.126 |
| B | 0.825 · 0.721 · 1.273 | 0.940 | 1.064 |
| C | 1.077 · 1.273 · 0.762 | 1.037 | 0.964 |
| D | 0.721 · 0.762 · 1.273 | 0.918 | 1.089 |
| E | 0.721 · 0.825 · 1.077 | 0.874 | 1.144 |
| F | 2.263 · 2.746 · 2.668 | 2.559 | 0.391 |
| G | 2.126 · 2.668 · 2.746 | 2.513 | 0.398 |
| H | 2.126 · 2.746 · 4.601 | 3.158 | 0.317 |
| I | 2.126 · 2.828 · 2.746 | 2.567 | 0.390 |
| J | 2.746 · 2.828 · 2.746 | 2.773 | 0.361 |
| P | 1.530 · 1.844 · 2.209 | 1.861 | 0.537 |
| Z | 2.828 · 4.535 · 4.601 | 3.988 | 0.251 |
LOF of P, by hand
- 1Neighbours of P: D (1.530), B (1.844), C (2.209), so .
- 2Reach-dists: ; ; . P lies outside all three k-circles, so the true distances are kept.
- 3.
- 4Neighbours' densities: , , , mean .
- 5. P is about twice as sparse as the cluster it borders, so it is an outlier, even though its k-distance is below that of most loose-cluster points.
LOF of Z, by hand
- 1Neighbours of Z: H (2.828), P (4.535), J (4.601), so , the largest in the data.
- 2Reach-dists: (Z is H's own 3rd neighbour, so the two coincide); ; .
- 3.
- 4Neighbours' densities: , mean .
- 5, also an outlier. It scores lower than P because its neighbours are themselves sparse, and LOF only measures sparseness relative to them.
| Point | neighbours' lrd | mean | own lrd | LOF |
|---|---|---|---|---|
| A | B 1.064 · E 1.144 · C 0.964 | 1.057 | 1.126 | 0.939 |
| B | A 1.126 · C 0.964 · D 1.089 | 1.060 | 1.064 | 0.996 |
| C | E 1.144 · D 1.089 · B 1.064 | 1.099 | 0.964 | 1.140 |
| D | C 0.964 · B 1.064 · A 1.126 | 1.052 | 1.089 | 0.966 |
| E | C 0.964 · A 1.126 · B 1.064 | 1.052 | 1.144 | 0.919 |
| F | J 0.361 · G 0.398 · P 0.537 | 0.432 | 0.391 | 1.105 |
| G | J 0.361 · F 0.391 · I 0.390 | 0.380 | 0.398 | 0.956 |
| H | J 0.361 · I 0.390 · Z 0.251 | 0.334 | 0.317 | 1.053 |
| I | J 0.361 · H 0.317 · G 0.398 | 0.358 | 0.390 | 0.920 |
| J | G 0.398 · H 0.317 · I 0.390 | 0.368 | 0.361 | 1.021 |
| P | D 1.089 · B 1.064 · C 0.964 | 1.039 | 0.537 | 1.934 |
| Z | H 0.317 · P 0.537 · J 0.361 | 0.405 | 0.251 | 1.615 |
The loose cluster's lrd is about a third of the tight cluster's (≈ 0.37 vs ≈ 1.08), yet every one of its points scores between 0.92 and 1.11. That is the whole point of LOF: density is judged against the neighbours, not against the whole dataset.
See it step by step
- 1for p in D: N_k(p) ← k nearest (not p)
- 2 k-dist(p) ← d(p, k-th neighbour)
- 3reach(p,o) ← max(k-dist(o), d(p,o))
- 4lrd(p) ← 1 / mean_{o∈N_k(p)} reach(p,o)
- 5LOF(p) ← mean_{o∈N_k(p)} lrd(o) / lrd(p)
- 6LOF ≈ 1 → inlier; LOF > 1.5 → outlier
- 7new x: same formulas, training fixed
12 points: a tight cluster A–E, a loose cluster F–J, P just outside the tight one and Z far from everything. k = 3. We follow P.
Scoring new points (novelty detection)
LOF as described scores the training points themselves (outlier detection: the training set is assumed to contain some outliers). To score an unseen point against clean training data (novelty detection), keep everything learned from the training set fixed (its k-distances and lrd values) and apply the same formulas to . Find its nearest training points, compute , then , then the ratio. The new point does not change anyone else's score.
| New point | neighbours | reach-dists | lrd | mean nbr lrd | LOF | verdict |
|---|---|---|---|---|---|---|
| (4.6, 1.8) | P · F · D | 2.209 · 2.668 · 2.702 | 0.396 | 0.672 | 1.699 | outlier (decision −0.199) |
| (7.5, 3.5) | J · H · I | 2.126 · 2.828 · 2.746 | 0.390 | 0.356 | 0.913 | inlier (decision 0.587) |
The algorithm
LOF(D, k)
- 1Neighbours. For each point , find its nearest other points and , using brute force, a KD-tree or a ball tree.
- 2Reach. For each neighbour of : .
- 3Density. . This needs every k-distance first, so it is a second pass.
- 4Score. . This needs every lrd first, so it is a third pass.
- 5Decide. Rank by LOF, or flag points above a threshold (1.5 by default in sklearn, or the top
contaminationfraction).
Choosing k
sets the scale of "local". If it is too small, the density estimates are noisy, and a pair of points that sit close together can shield each other: each has the other as a dense neighbour. If it is too large, the neighbourhoods spill across cluster boundaries and LOF turns into a global method. In the example, P scores 2.16, 2.32, 1.93 and 1.71 for , then 0.98 at . The tight cluster has only five members, so each of their 5-neighbourhoods must include P. Their densities drop to P's level and P looks normal.
In code
1import numpy as np23def lof_fit(X, k):4 D = np.linalg.norm(X[:, None] - X[None], axis=2) # pairwise distances5 np.fill_diagonal(D, np.inf) # a point is not its own neighbour6 nbrs = np.argsort(D, axis=1, kind="stable")[:, :k] # N_k(p), ties by index7 rows = np.arange(len(X))[:, None]8 kdist = D[rows[:, 0], nbrs[:, -1]] # distance to the k-th neighbour9 reach = np.maximum(kdist[nbrs], D[rows, nbrs]) # reach-dist(p, o)10 lrd = 1 / reach.mean(axis=1) # local reachability density11 lof = lrd[nbrs].mean(axis=1) / lrd # neighbours' density / own density12 return dict(nbrs=nbrs, kdist=kdist, lrd=lrd, lof=lof)1314def lof_score_new(X, fit, x, k):15 d = np.linalg.norm(X - x, axis=1)16 nb = np.argsort(d, kind="stable")[:k]17 lrd_x = 1 / np.maximum(fit["kdist"][nb], d[nb]).mean()18 return fit["lrd"][nb].mean() / lrd_x1920X = np.array([[1.0, 1.0], [1.6, 1.2], [1.2, 1.8], [1.9, 1.9], [0.6, 1.6], # A-E tight21 [6.0, 1.0], [8.5, 1.5], [6.5, 4.0], [9.0, 4.2], [7.6, 2.6], # F-J loose22 [3.4, 1.6], [4.5, 6.0]]) # P, Z23fit = lof_fit(X, k=3)24print(fit["lof"].round(3))25# [0.939 0.996 1.14 0.966 0.919 1.105 0.956 1.053 0.92 1.021 1.934 1.615]26print([round(lof_score_new(X, fit, np.array(x), 3), 3) for x in [(4.6, 1.8), (7.5, 3.5)]])27# [1.699, 0.913]1from sklearn.neighbors import LocalOutlierFactor23# Outlier detection: score the data you fit on.4lof = LocalOutlierFactor(n_neighbors=3) # contamination="auto" → flag LOF > 1.55labels = lof.fit_predict(X) # +1 inlier, -1 outlier6print(labels) # [ 1 1 1 1 1 1 1 1 1 1 -1 -1] → P and Z7print(-lof.negative_outlier_factor_) # the LOF scores (stored negated)8print(lof.offset_) # -1.5910# Novelty detection: fit on (clean) data, then score unseen points.11nov = LocalOutlierFactor(n_neighbors=3, novelty=True).fit(X)12X_new = np.array([[4.6, 1.8], [7.5, 3.5]])13print(nov.predict(X_new)) # [-1 1]14print(-nov.score_samples(X_new)) # [1.699 0.913]15print(nov.decision_function(X_new)) # [-0.199 0.587] = score_samples - offset_1617# In practice: scale first, and keep k well above the size of tiny clusters.18# from sklearn.pipeline import make_pipeline; from sklearn.preprocessing import StandardScaler19# model = make_pipeline(StandardScaler(), LocalOutlierFactor(n_neighbors=20, novelty=True))Use when
- +The data mixes regions of very different density and you care about points that are odd for their region: local outliers.
- +You want an interpretable score: "this point is 1.9× sparser than its neighbours".
- +There are no labels, and the clusters have any shape. No Gaussian or convexity assumption is needed.
- +The dataset is small to medium and low to moderate in dimension, where k-NN queries are cheap and distances meaningful.
Avoid when
- −The data is large: every fit needs all-pairs k-NN, and the whole training set must be kept for scoring.
- −The dimension is high: distances concentrate and density estimates break down. Isolation forest usually copes better.
- −You need a calibrated probability or a universal threshold: LOF values are relative and dataset-dependent.
- −Outliers come in clumps larger than k: a tight group of anomalies looks like a normal cluster to LOF.
Where it's used
Fraud and intrusion detection
Transactions or network sessions that are unusual for that customer segment or host, not just globally rare.
Sensor and IoT monitoring
Readings that drift away from the local operating regime of a machine running in several modes.
Data cleaning
Flag suspicious rows (entry errors, unit mix-ups) before training a supervised model.
Scientific data
Unusual stars, cells or samples within populations of very different spread.
Check yourself
A point has and its three neighbours have . Its LOF is…
Check yourself
and . What is ?
Check yourself
Every point of a sparse but evenly spread cluster has a large k-distance. What LOF scores do they get?
Check yourself
You fit LocalOutlierFactor(novelty=True) on training data and want scores for the training points. What should you do?
Remember
- ✓LOF compares a point's density with its neighbours' density: means normal, means sparser than its surroundings.
- ✓Chain: k-distance → → → .
- ✓Because it is a ratio, LOF finds local outliers next to dense clusters while leaving uniformly sparse clusters alone. A global distance threshold cannot do both.
- ✓ sets the scale of "local". Keep it above the size of small groups you would call outliers (but not larger than real clusters), or take the max over a range of .
- ✓Scale the features first. For new points use
novelty=Truewithpredict/score_samples; for training points usefit_predictandnegative_outlier_factor_.