# 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.

Source: https://www.mlfoundry.in/learn/local-outlier-factor  
Section: ML Algorithms › Unsupervised · Anomaly Detection  
Level: core · 16 min read · updated 2026-10-08

**The local outlier factor (LOF):** of a point is the average density of its $k$ 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*.

- **Task:** Unsupervised anomaly (outlier) detection; novelty detection on new points
- **Input → output:** points $x\in\mathbb{R}^d$ → a score $\text{LOF}(x)\ge 0$, with $\approx 1$ normal and $\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(n^2 d)$ brute force, roughly $O(n\log n)$ with a tree index in low dimensions
- **Hyperparameters:** $k$ (`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 $k$ 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 $k$ and a distance $d$ (usually Euclidean). Every quantity below is built from the previous one.

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

$$
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)\,\}
$$

- $o_k(p)$: the $k$-th nearest neighbour of $p$ (not counting $p$ itself)
- $k\text{-dist}(p)$: small in dense regions, large in sparse ones
- $N_k(p)$: the k-neighbourhood of $p$. It has exactly $k$ 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* $p$ *to* a neighbour $o$ is never allowed to be smaller than $o$'s own k-distance.

$$
\text{reach-dist}_k(p,o)=\max\{\,k\text{-dist}(o),\;d(p,o)\,\}
$$

- $d(p,o)$: the true distance, used when $p$ lies outside $o$'s k-circle
- $k\text{-dist}(o)$: used when $p$ lies inside $o$'s k-circle: all such points look equally close to $o$

> **It is not symmetric:** $\text{reach-dist}(p,o)$ uses the k-distance of the *target* $o$, so in general $\text{reach-dist}(p,o)\ne\text{reach-dist}(o,p)$. It is not a metric, only a smoothed "how far is $o$ from where $p$ stands".

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

$$
\text{lrd}_k(p)=\left(\frac{1}{|N_k(p)|}\sum_{o\in N_k(p)}\text{reach-dist}_k(p,o)\right)^{-1}
$$

- $\text{lrd}_k(p)$: local reachability density, in points per unit distance (higher = more crowded)
- $|N_k(p)|$: number of neighbours, normally $k$

**4. Local outlier factor.** Compare $p$'s density with the densities of its neighbours.

$$
\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)}
$$

- $\text{LOF}_k(p)\approx 1$: p is as dense as its neighbours: an inlier, whether the region is tight or loose
- $\text{LOF}_k(p)<1$: p is denser than its neighbours, for example near the core of a cluster
- $\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 $\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)$ and a **loose** cluster F–J about 2 units apart. Then two suspects: **P** at $(3.4, 1.6)$ sits about 1.5 units from the tight cluster, and **Z** at $(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).

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.

| 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** |

> **Why a global rule fails here:** Rank points by raw k-distance and you get Z, H, I, G, F, then P. Four perfectly normal members of the loose cluster look *more* anomalous than P. Any single distance threshold that catches P also flags most of the loose cluster. LOF fixes this by dividing by the neighbours' density.

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.

| 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**

1. Neighbours of P: D (1.530), B (1.844), C (2.209), so $k\text{-dist}(P)=2.209$.
2. Reach-dists: $\max(k\text{-dist}(D)=1.273,\,1.530)=1.530$; $\max(0.762,\,1.844)=1.844$; $\max(0.721,\,2.209)=2.209$. P lies outside all three k-circles, so the true distances are kept.
3. $\text{lrd}(P)=1\big/\tfrac{1.530+1.844+2.209}{3}=1/1.861=0.537$.
4. Neighbours' densities: $\text{lrd}(D)=1.089$, $\text{lrd}(B)=1.064$, $\text{lrd}(C)=0.964$, mean $1.039$.
5. $\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. Neighbours of Z: H (2.828), P (4.535), J (4.601), so $k\text{-dist}(Z)=4.601$, the largest in the data.
2. Reach-dists: $\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.126,\,4.601)=4.601$.
3. $\text{lrd}(Z)=1/3.988=0.251$.
4. Neighbours' densities: $0.317,\ 0.537,\ 0.361$, mean $0.405$.
5. $\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.

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).

| 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

> Figure: 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 $x$ 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 $x$. Find its $k$ nearest training points, compute $\text{reach-dist}(x,o)=\max(k\text{-dist}(o),d(x,o))$, then $\text{lrd}(x)$, then the ratio. The new point does not change anyone else's score.

Two new points scored against the 12-point model (k = 3). sklearn's decision_function equals 1.5 − LOF, so negative means outlier.

| 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) |

> **Don't score the training set in novelty mode:** With `novelty=True`, calling `predict` or `score_samples` on the training data is wrong: each training point finds *itself* as a neighbour at distance 0, so its density is overstated. scikit-learn warns about this. Use `negative_outlier_factor_` for the training points and `predict` only for new data. Conversely, the default `novelty=False` model has no `predict` method at all, only `fit_predict`.

## The algorithm

**LOF(D, k)**

1. **Neighbours.** For each point $p$, find its $k$ nearest other points $N_k(p)$ and $k\text{-dist}(p)$, using brute force, a KD-tree or a ball tree.
2. **Reach.** For each neighbour $o$ of $p$: $\text{reach-dist}(p,o)=\max(k\text{-dist}(o),d(p,o))$.
3. **Density.** $\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. **Score.** $\text{LOF}(p)=\operatorname{mean}_o\text{lrd}(o)/\text{lrd}(p)$. This needs every lrd first, so it is a third pass.
5. **Decide.** Rank by LOF, or flag points above a threshold (1.5 by default in sklearn, or the top `contamination` fraction).

## Choosing k

$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 $k=1\dots4$, then **0.98 at $k=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.

> **Rules of thumb:** Breunig et al. suggest a lower bound of about 10 (to keep the density estimates stable) and an upper bound equal to the largest group of points you would still call outliers. scikit-learn defaults to `n_neighbors=20`. A robust option is to compute LOF for a range of $k$ and keep each point's **maximum** score.

## In code

```python
import numpy as np

def lof_fit(X, k):
    D = np.linalg.norm(X[:, None] - X[None], axis=2)      # pairwise distances
    np.fill_diagonal(D, np.inf)                            # a point is not its own neighbour
    nbrs = np.argsort(D, axis=1, kind="stable")[:, :k]     # N_k(p), ties by index
    rows = np.arange(len(X))[:, None]
    kdist = D[rows[:, 0], nbrs[:, -1]]                     # distance to the k-th neighbour
    reach = np.maximum(kdist[nbrs], D[rows, nbrs])         # reach-dist(p, o)
    lrd = 1 / reach.mean(axis=1)                           # local reachability density
    lof = lrd[nbrs].mean(axis=1) / lrd                     # neighbours' density / own density
    return dict(nbrs=nbrs, kdist=kdist, lrd=lrd, lof=lof)

def lof_score_new(X, fit, x, k):
    d = np.linalg.norm(X - x, axis=1)
    nb = np.argsort(d, kind="stable")[:k]
    lrd_x = 1 / np.maximum(fit["kdist"][nb], d[nb]).mean()
    return fit["lrd"][nb].mean() / lrd_x

X = 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
              [6.0, 1.0], [8.5, 1.5], [6.5, 4.0], [9.0, 4.2], [7.6, 2.6],   # F-J loose
              [3.4, 1.6], [4.5, 6.0]])                                      # P, Z
fit = lof_fit(X, k=3)
print(fit["lof"].round(3))
# [0.939 0.996 1.14  0.966 0.919 1.105 0.956 1.053 0.92  1.021 1.934 1.615]
print([round(lof_score_new(X, fit, np.array(x), 3), 3) for x in [(4.6, 1.8), (7.5, 3.5)]])
# [1.699, 0.913]
```

From scratch in NumPy. It reproduces every number in the tables above.

```python
from sklearn.neighbors import LocalOutlierFactor

# Outlier detection: score the data you fit on.
lof = LocalOutlierFactor(n_neighbors=3)        # contamination="auto" → flag LOF > 1.5
labels = lof.fit_predict(X)                    # +1 inlier, -1 outlier
print(labels)                     # [ 1  1  1  1  1  1  1  1  1  1 -1 -1]  → P and Z
print(-lof.negative_outlier_factor_)           # the LOF scores (stored negated)
print(lof.offset_)                # -1.5

# Novelty detection: fit on (clean) data, then score unseen points.
nov = LocalOutlierFactor(n_neighbors=3, novelty=True).fit(X)
X_new = np.array([[4.6, 1.8], [7.5, 3.5]])
print(nov.predict(X_new))                      # [-1  1]
print(-nov.score_samples(X_new))               # [1.699 0.913]
print(nov.decision_function(X_new))            # [-0.199  0.587]  = score_samples - offset_

# In practice: scale first, and keep k well above the size of tiny clusters.
# from sklearn.pipeline import make_pipeline; from sklearn.preprocessing import StandardScaler
# model = make_pipeline(StandardScaler(), LocalOutlierFactor(n_neighbors=20, novelty=True))
```

scikit-learn: outlier detection on the training set, then novelty=True for new points.

> **Sign conventions in scikit-learn:** `negative_outlier_factor_` and `score_samples` return **−LOF**, so higher means more normal, matching the other sklearn anomaly detectors. `decision_function` subtracts `offset_` (−1.5 with `contamination="auto"`, otherwise the chosen percentile), so its zero is the decision boundary.

**Strengths**

- 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.

**Limitations**

- 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.

- **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.

> **Unscaled features:** LOF is purely distance-based. A feature measured in thousands swamps one measured in fractions, and the neighbourhoods become one-dimensional. Standardize, or use a metric that fits the data, before fitting.

> **Duplicate points:** If at least $k$ copies of a point exist, its k-distance is 0, every reach-dist to it can be 0 and its lrd becomes infinite. sklearn adds $10^{-10}$ to avoid division by zero, but nearby points then get huge or meaningless scores. De-duplicate (or add a count feature), or choose $k$ larger than the largest number of duplicates.

**Question:** A point has $\text{lrd}=0.5$ and its three neighbours have $\text{lrd}=1.0,\,1.2,\,1.1$. Its LOF is…

- A. 0.45
- B. 1.1
- C. 2.2
- D. 0.55

**Answer:** C. 2.2 — Mean neighbour lrd is $1.1$, so $\text{LOF}=1.1/0.5=2.2$. The point is about twice as sparse as its neighbours, so it is an outlier.

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

- A. 0.5
- B. 0.65
- C. 0.8
- D. 1.3

**Answer:** C. 0.8 — $\max(0.8,\,0.5)=0.8$. Because $p$ lies inside $o$'s k-circle, its distance is rounded up to $o$'s k-distance. This smoothing keeps lrd from blowing up for points packed very close together.

**Question:** Every point of a sparse but evenly spread cluster has a large k-distance. What LOF scores do they get?

- A. All well above 1, since they are far from their neighbours
- B. Close to 1, since their neighbours are just as sparse
- C. Close to 0
- D. It depends only on the global average density

**Answer:** B. Close to 1, since their neighbours are just as sparse — LOF is a ratio of the neighbours' density to the point's own density. Inside a uniformly sparse cluster both are low, so the ratio is about 1, like F–J in the worked example.

**Question:** You fit `LocalOutlierFactor(novelty=True)` on training data and want scores for the training points. What should you do?

- A. Call `predict(X_train)`
- B. Call `score_samples(X_train)`
- C. Fit a separate model with `novelty=False` and read `negative_outlier_factor_`
- D. Call `fit_predict(X_train)` on the novelty model

**Answer:** C. Fit a separate model with `novelty=False` and read `negative_outlier_factor_` — In novelty mode each training point would find itself as a neighbour at distance 0, which inflates its density. Training-set scores come from `negative_outlier_factor_` of an outlier-detection (`novelty=False`) fit, and `fit_predict` is not available in novelty mode.

**Key points**

- LOF compares a point's density with its neighbours' density: $\approx 1$ means normal, $\gg 1$ means sparser than its surroundings.
- Chain: k-distance → $\text{reach-dist}(p,o)=\max(k\text{-dist}(o),d(p,o))$ → $\text{lrd}=1/\text{mean reach}$ → $\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.
- $k$ 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 $k$.
- Scale the features first. For new points use `novelty=True` with `predict` / `score_samples`; for training points use `fit_predict` and `negative_outlier_factor_`.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
