Local Outlier Factor

Compare a point's local density with its neighbours'; a ratio well above 1 means it sits in a sparser region than they do.

16 min read1295 wordscore0 viewsupdated 2026-10-08

Definition

The local outlier factor (LOF) of a point is the average density of its kk 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 x∈Rdx\in\mathbb{R}^d → a score LOF(x)≥0\text{LOF}(x)\ge 0, with ≈1\approx 1 normal and ≫1\gg 1 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: O(n2d)O(n^2 d) brute force, roughly O(nlog⁡n)O(n\log n) with a tree index in low dimensions
Hyperparameters
kk (n_neighbors), the distance metric, and the threshold or contamination
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 kk 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 kk and a distance dd (usually Euclidean). Every quantity below is built from the previous one.

1. k-distance. Sort the other points by distance from pp. The kk closest form the neighbourhood Nk(p)N_k(p), and the distance to the kk-th of them is the radius of the smallest circle around pp that holds kk neighbours.

k-dist(p)=d(p,  ok(p)),Nk(p)={ o≠p: d(p,o)≤k-dist(p) }k\text{-dist}(p)=d\big(p,\;o_k(p)\big),\qquad N_k(p)=\{\,o\ne p:\ d(p,o)\le k\text{-dist}(p)\,\}
ok(p)o_k(p)
the kk-th nearest neighbour of pp (not counting pp itself)
k-dist(p)k\text{-dist}(p)
small in dense regions, large in sparse ones
Nk(p)N_k(p)
the k-neighbourhood of pp. It has exactly kk 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 pp to a neighbour oo is never allowed to be smaller than oo's own k-distance.

reach-distk(p,o)=max⁡{ k-dist(o),  d(p,o) }\text{reach-dist}_k(p,o)=\max\{\,k\text{-dist}(o),\;d(p,o)\,\}
d(p,o)d(p,o)
the true distance, used when pp lies outside oo's k-circle
k-dist(o)k\text{-dist}(o)
used when pp lies inside oo's k-circle: all such points look equally close to oo

3. Local reachability density. Average the reach-distances from pp to its neighbours and take the reciprocal. Short reaches mean high density.

lrdk(p)=(1∣Nk(p)∣∑o∈Nk(p)reach-distk(p,o))−1\text{lrd}_k(p)=\left(\frac{1}{|N_k(p)|}\sum_{o\in N_k(p)}\text{reach-dist}_k(p,o)\right)^{-1}
lrdk(p)\text{lrd}_k(p)
local reachability density, in points per unit distance (higher = more crowded)
∣Nk(p)∣|N_k(p)|
number of neighbours, normally kk

4. Local outlier factor. Compare pp's density with the densities of its neighbours.

LOFk(p)=1∣Nk(p)∣∑o∈Nk(p)lrdk(o)lrdk(p)=mean⁡o∈Nk(p)lrdk(o)lrdk(p)\text{LOF}_k(p)=\frac{1}{|N_k(p)|}\sum_{o\in N_k(p)}\frac{\text{lrd}_k(o)}{\text{lrd}_k(p)}=\frac{\operatorname{mean}_{o\in N_k(p)}\text{lrd}_k(o)}{\text{lrd}_k(p)}
LOFk(p)≈1\text{LOF}_k(p)\approx 1
p is as dense as its neighbours: an inlier, whether the region is tight or loose
LOFk(p)<1\text{LOF}_k(p)<1
p is denser than its neighbours, for example near the core of a cluster
LOFk(p)≫1\text{LOF}_k(p)\gg 1
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 LOF>1.5\text{LOF}>1.5, 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 (1.3,1.5)(1.3, 1.5) and a loose cluster F–J about 2 units apart. Then two suspects: P at (3.4,1.6)(3.4, 1.6) sits about 1.5 units from the tight cluster, and Z at (4.5,6.0)(4.5, 6.0) 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.8250.825
B(1.6, 1.2)A 0.632 · C 0.721 · D 0.7620.762
C(1.2, 1.8)E 0.632 · D 0.707 · B 0.7210.721
D(1.9, 1.9)C 0.707 · B 0.762 · A 1.2731.273
E(0.6, 1.6)C 0.632 · A 0.721 · B 1.0771.077
F(6.0, 1.0)J 2.263 · G 2.550 · P 2.6682.668
G(8.5, 1.5)J 1.421 · F 2.550 · I 2.7462.746
H(6.5, 4.0)J 1.780 · I 2.508 · Z 2.8282.828
I(9.0, 4.2)J 2.126 · H 2.508 · G 2.7462.746
J(7.6, 2.6)G 1.421 · H 1.780 · I 2.1262.126
P(3.4, 1.6)D 1.530 · B 1.844 · C 2.2092.209
Z(4.5, 6.0)H 2.828 · P 4.535 · J 4.6014.601
Step 1: neighbours and k-distance. Highlighted: P and Z. Note that P's k-distance (2.209) is smaller than that of every loose-cluster point F, G, H, I.
Pointreach-dist to the 3 neighboursmeanlrd
A0.762 · 1.077 · 0.8250.8881.126
B0.825 · 0.721 · 1.2730.9401.064
C1.077 · 1.273 · 0.7621.0370.964
D0.721 · 0.762 · 1.2730.9181.089
E0.721 · 0.825 · 1.0770.8741.144
F2.263 · 2.746 · 2.6682.5590.391
G2.126 · 2.668 · 2.7462.5130.398
H2.126 · 2.746 · 4.6013.1580.317
I2.126 · 2.828 · 2.7462.5670.390
J2.746 · 2.828 · 2.7462.7730.361
P1.530 · 1.844 · 2.2091.8610.537
Z2.828 · 4.535 · 4.6013.9880.251
Step 2: reach-dists (in the same neighbour order as above), their mean, and lrd = 1 / mean. A's first entry is 0.762, not its true distance 0.632, because A lies inside B's k-circle.

LOF of P, by hand

  1. 1Neighbours of P: D (1.530), B (1.844), C (2.209), so k-dist(P)=2.209k\text{-dist}(P)=2.209.
  2. 2Reach-dists: max⁡(k-dist(D)=1.273, 1.530)=1.530\max(k\text{-dist}(D)=1.273,\,1.530)=1.530; max⁡(0.762, 1.844)=1.844\max(0.762,\,1.844)=1.844; max⁡(0.721, 2.209)=2.209\max(0.721,\,2.209)=2.209. P lies outside all three k-circles, so the true distances are kept.
  3. 3lrd(P)=1/1.530+1.844+2.2093=1/1.861=0.537\text{lrd}(P)=1\big/\tfrac{1.530+1.844+2.209}{3}=1/1.861=0.537.
  4. 4Neighbours' densities: lrd(D)=1.089\text{lrd}(D)=1.089, lrd(B)=1.064\text{lrd}(B)=1.064, lrd(C)=0.964\text{lrd}(C)=0.964, mean 1.0391.039.
  5. 5LOF(P)=1.039/0.537=1.934\text{LOF}(P)=1.039/0.537=1.934. 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

  1. 1Neighbours of Z: H (2.828), P (4.535), J (4.601), so k-dist(Z)=4.601k\text{-dist}(Z)=4.601, the largest in the data.
  2. 2Reach-dists: max⁡(k-dist(H)=2.828, 2.828)=2.828\max(k\text{-dist}(H)=2.828,\,2.828)=2.828 (Z is H's own 3rd neighbour, so the two coincide); max⁡(2.209, 4.535)=4.535\max(2.209,\,4.535)=4.535; max⁡(2.126, 4.601)=4.601\max(2.126,\,4.601)=4.601.
  3. 3lrd(Z)=1/3.988=0.251\text{lrd}(Z)=1/3.988=0.251.
  4. 4Neighbours' densities: 0.317, 0.537, 0.3610.317,\ 0.537,\ 0.361, mean 0.4050.405.
  5. 5LOF(Z)=0.405/0.251=1.615\text{LOF}(Z)=0.405/0.251=1.615, also an outlier. It scores lower than P because its neighbours are themselves sparse, and LOF only measures sparseness relative to them.
Pointneighbours' lrdmeanown lrdLOF
AB 1.064 · E 1.144 · C 0.9641.0571.1260.939
BA 1.126 · C 0.964 · D 1.0891.0601.0640.996
CE 1.144 · D 1.089 · B 1.0641.0990.9641.140
DC 0.964 · B 1.064 · A 1.1261.0521.0890.966
EC 0.964 · A 1.126 · B 1.0641.0521.1440.919
FJ 0.361 · G 0.398 · P 0.5370.4320.3911.105
GJ 0.361 · F 0.391 · I 0.3900.3800.3980.956
HJ 0.361 · I 0.390 · Z 0.2510.3340.3171.053
IJ 0.361 · H 0.317 · G 0.3980.3580.3900.920
JG 0.398 · H 0.317 · I 0.3900.3680.3611.021
PD 1.089 · B 1.064 · C 0.9641.0390.5371.934
ZH 0.317 · P 0.537 · J 0.3610.4050.2511.615
Step 3: LOF = mean neighbour lrd / own lrd. Only P and Z exceed 1.5, which is exactly what sklearn's fit_predict returns (−1 for P and Z, +1 for the rest).

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

follow point:
▸ LOF · worked examplestep 1/11
01234567890123456ABCDEFGHIJPZ
k = 3point P
  1. 1for p in D: N_k(p) ← k nearest (not p)
  2. 2 k-dist(p) ← d(p, k-th neighbour)
  3. 3reach(p,o) ← max(k-dist(o), d(p,o))
  4. 4lrd(p) ← 1 / mean_{o∈N_k(p)} reach(p,o)
  5. 5LOF(p) ← mean_{o∈N_k(p)} lrd(o) / lrd(p)
  6. 6LOF ≈ 1 → inlier; LOF > 1.5 → outlier
  7. 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.

▸ LOF explorerdrag any point · click to inspect
01234567890123456ABCDEFGHIJPZLOF, k = 3A0.94B1.00C1.14D0.97E0.92F1.11G0.96H1.05I0.92J1.02P1.93Z1.6111.5
P: Nk = D, B, C · k-dist 2.209 · reach 1.530, 1.844, 2.209
lrd 0.537 · neighbours' lrd 1.089, 1.064, 0.964 → LOF 1.934
outliers (LOF > 1.5): 2
fig.Top: follow one point (P, Z, A or H) through the calculation. The dashed circle is its k-distance, each neighbour's own k-circle shows why a reach-dist is rounded up (or not), the shaded discs have radius 1/lrd, and the last frames score every point and then two unseen points in novelty mode. Bottom: drag any point and the scores update; click a point to see its neighbourhood, reach-dists and lrd. Colour and size show LOF (green ≈ 1, rose > 1.5). Move the k slider to 5 and P's score collapses, because the 5-neighbourhoods of A–E must now reach out to 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 xx 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 xx. Find its kk nearest training points, compute reach-dist(x,o)=max⁡(k-dist(o),d(x,o))\text{reach-dist}(x,o)=\max(k\text{-dist}(o),d(x,o)), then lrd(x)\text{lrd}(x), then the ratio. The new point does not change anyone else's score.

New pointneighboursreach-distslrdmean nbr lrdLOFverdict
(4.6, 1.8)P · F · D2.209 · 2.668 · 2.7020.3960.6721.699outlier (decision −0.199)
(7.5, 3.5)J · H · I2.126 · 2.828 · 2.7460.3900.3560.913inlier (decision 0.587)
Two new points scored against the 12-point model (k = 3). sklearn's decision_function equals 1.5 − LOF, so negative means outlier.

The algorithm

LOF(D, k)

  1. 1Neighbours. For each point pp, find its kk nearest other points Nk(p)N_k(p) and k-dist(p)k\text{-dist}(p), using brute force, a KD-tree or a ball tree.
  2. 2Reach. For each neighbour oo of pp: reach-dist(p,o)=max⁡(k-dist(o),d(p,o))\text{reach-dist}(p,o)=\max(k\text{-dist}(o),d(p,o)).
  3. 3Density. lrd(p)=1/mean⁡oreach-dist(p,o)\text{lrd}(p)=1/\operatorname{mean}_o\text{reach-dist}(p,o). This needs every k-distance first, so it is a second pass.
  4. 4Score. LOF(p)=mean⁡olrd(o)/lrd(p)\text{LOF}(p)=\operatorname{mean}_o\text{lrd}(o)/\text{lrd}(p). This needs every lrd first, so it is a third pass.
  5. 5Decide. Rank by LOF, or flag points above a threshold (1.5 by default in sklearn, or the top contamination fraction).

Choosing k

kk 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 k=1…4k=1\dots4, then 0.98 at k=5k=5. 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

python
1import numpy as np
2
3def lof_fit(X, k):
4 D = np.linalg.norm(X[:, None] - X[None], axis=2) # pairwise distances
5 np.fill_diagonal(D, np.inf) # a point is not its own neighbour
6 nbrs = np.argsort(D, axis=1, kind="stable")[:, :k] # N_k(p), ties by index
7 rows = np.arange(len(X))[:, None]
8 kdist = D[rows[:, 0], nbrs[:, -1]] # distance to the k-th neighbour
9 reach = np.maximum(kdist[nbrs], D[rows, nbrs]) # reach-dist(p, o)
10 lrd = 1 / reach.mean(axis=1) # local reachability density
11 lof = lrd[nbrs].mean(axis=1) / lrd # neighbours' density / own density
12 return dict(nbrs=nbrs, kdist=kdist, lrd=lrd, lof=lof)
13
14def 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_x
19
20X = np.array([[1.0, 1.0], [1.6, 1.2], [1.2, 1.8], [1.9, 1.9], [0.6, 1.6], # A-E tight
21 [6.0, 1.0], [8.5, 1.5], [6.5, 4.0], [9.0, 4.2], [7.6, 2.6], # F-J loose
22 [3.4, 1.6], [4.5, 6.0]]) # P, Z
23fit = 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]
From scratch in NumPy. It reproduces every number in the tables above.
python
1from sklearn.neighbors import LocalOutlierFactor
2
3# Outlier detection: score the data you fit on.
4lof = LocalOutlierFactor(n_neighbors=3) # contamination="auto" → flag LOF > 1.5
5labels = lof.fit_predict(X) # +1 inlier, -1 outlier
6print(labels) # [ 1 1 1 1 1 1 1 1 1 1 -1 -1] → P and Z
7print(-lof.negative_outlier_factor_) # the LOF scores (stored negated)
8print(lof.offset_) # -1.5
9
10# 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_
16
17# 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 StandardScaler
19# model = make_pipeline(StandardScaler(), LocalOutlierFactor(n_neighbors=20, novelty=True))
scikit-learn: outlier detection on the training set, then novelty=True for new points.

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 lrd=0.5\text{lrd}=0.5 and its three neighbours have lrd=1.0, 1.2, 1.1\text{lrd}=1.0,\,1.2,\,1.1. Its LOF is…

Check yourself

k-dist(o)=0.8k\text{-dist}(o)=0.8 and d(p,o)=0.5d(p,o)=0.5. What is reach-distk(p,o)\text{reach-dist}_k(p,o)?

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: ≈1\approx 1 means normal, ≫1\gg 1 means sparser than its surroundings.
  • ✓Chain: k-distance → reach-dist(p,o)=max⁡(k-dist(o),d(p,o))\text{reach-dist}(p,o)=\max(k\text{-dist}(o),d(p,o)) → lrd=1/mean reach\text{lrd}=1/\text{mean reach} → LOF=mean neighbour lrd/own lrd\text{LOF}=\text{mean neighbour lrd}/\text{own lrd}.
  • ✓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.
  • ✓kk 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 kk.
  • ✓Scale the features first. For new points use novelty=True with predict / score_samples; for training points use fit_predict and negative_outlier_factor_.
← back to ML Algorithms roadmap