Skip to content
Tutorials Center
My learning

Topics

Lesson 71 of 111 ยท Unsupervised Learning

Anomaly and Outlier Detection

Find unusual points with IsolationForest, LocalOutlierFactor, OneClassSVM and EllipticEnvelope, understand contamination, and evaluate detectors when you have no labels.

Some of the most valuable rows in a dataset are the strangest ones: a card payment that looks nothing like the owner's history, a sensor reading from a machine about to fail, a login from an impossible location. Anomaly detection (also called outlier detection) tries to find such points, usually without any labels, because real anomalies are rare, varied and often not seen before.

scikit-learn offers several detectors, each with its own idea of "unusual". This lesson compares four of them on the same data, explains the contamination setting that confuses many people, and shows how to judge a detector when nobody has told you which points are anomalies. It continues the unsupervised chapter after Gaussian Mixture Models, which can also be used for density-based anomaly scores.

Four ways to say "unusual"

IsolationForest Odd points are easy to isolate.
LocalOutlierFactor Much sparser than its neighbours.
EllipticEnvelope Far from a fitted Gaussian.
OneClassSVM Outside a learned boundary.
1 / 2

The detectors

IsolationForest builds many random trees that split on random features at random values. An anomaly sits in a sparse region, so it gets isolated from the rest after only a few splits. The average path length across the forest is the score: short means odd. It is fast, needs no scaling for most uses, and is a strong default.

LocalOutlierFactor (LOF) compares the density around a point with the density around its n_neighbors nearest neighbours. A point in a thin region next to a dense cluster gets a high factor. Because it is local, it can catch outliers near a tight cluster that a global method would miss. It relies on distances, so scale your features.

EllipticEnvelope assumes the normal data is roughly Gaussian, fits a robust covariance estimate and flags points with a large Mahalanobis distance. It is excellent for a single blob and wrong for multi-modal or curved data.

OneClassSVM learns a boundary around the training points with a kernel, as in kernel SVMs, and calls everything outside it anomalous. It is sensitive to gamma and nu and to scaling, and it gets slow on large data.

All four share one interface: fit_predict(X) returns 1 for normal points and -1 for anomalies, and a continuous score is available too (score_samples or decision_function; for LOF, negative_outlier_factor_).

Python Four detectors on the same data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On a single, roughly Gaussian blob with scattered noise, three of the four do a reasonable job. Three of them flag exactly 16 of the 315 points (5%) because we told them to expect that share; the one-class SVM, whose nu is only an upper bound on training errors, flagged 18 and found far fewer true anomalies, a hint that it needs tuning of gamma and nu. The real anomalies that fall close to the blob by chance cannot be found by any method, so a perfect score is not expected.

What contamination means

contamination is your guess of the fraction of anomalies in the data. It does not change the model's scores; it only sets the threshold. The detector ranks every point by anomaly score, then labels the top contamination fraction as -1. (OneClassSVM uses nu for a similar purpose.) Two consequences follow:

  • If you set contamination=0.05, about 5% of the points are flagged no matter what, even on perfectly clean data.
  • If you do not know the rate, think in terms of scores and a review budget: "show me the 20 most unusual rows per day". Rank by score and cut at a number you can act on.

Local versus global anomalies

A global method judges a point against the whole dataset. A local method judges it against its neighbourhood. This matters when densities differ. The "suspect" point below sits just outside a very tight cluster: close to everything in absolute terms, but far for that cluster. The "edge" point lies at the fringe of a broad cluster: far from its centre, but ordinary for a cluster that wide.

Python A subtle outlier next to a tight cluster

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

LOF ranks the suspect second-most anomalous in the whole dataset (factor around 6) and the edge point as unremarkable (factor around 1.4, rank 36). IsolationForest ranks the two points almost alike, around 8th to 10th, because both sit in sparse regions in absolute terms. Neither is "wrong": they answer different questions, and the right choice depends on what an anomaly means in your problem.

Outlier detection versus novelty detection

In outlier detection you fit and predict on the same data that contains the anomalies. In novelty detection you fit on data you believe is clean and then judge new points. IsolationForest, OneClassSVM and EllipticEnvelope support predict on new data directly. LocalOutlierFactor only supports predict after you construct it with novelty=True, and then you must not call fit_predict for scoring training data. Novelty detection is the usual fit for production monitoring: train on last month's normal behaviour, score today's events.

Evaluating without labels

If you have no labels, accuracy is impossible, but you still have options.

  1. Plant anomalies. Inject synthetic outliers (extreme values, swapped columns, shifted distributions) into held-out clean data and measure how many the detector finds. This tests sensitivity, not realism.
  2. Review the top of the list. Have a domain expert examine the 50 highest-scoring rows. Even a rough "precision at 50" is valuable.
  3. Check stability. Run with different seeds or on bootstrap samples; the same points should keep showing up. A detector whose top list changes completely each run is not trustworthy.
  4. Compare detectors. If different methods with different assumptions agree on a point, it is probably worth a look.
  5. When you get labels later, evaluate by ranking quality, using ROC and precision-recall curves on the anomaly score, not accuracy: anomalies are extremely rare, so a model that flags nothing is 99.9% accurate.
Python Evaluating by planted anomalies and score stability

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A high ROC AUC together with a strong average precision (far above the 5% base rate) shows that the detector ranks planted anomalies well, and overlap close to 1 shows that the ranking does not depend on the random seed. Remember what this does and does not prove: it tests sensitivity to the kind of anomaly you planted, not to the strange events real life will produce.

Practical tips

  • Start with IsolationForest; add LOF when local density matters and EllipticEnvelope for simple unimodal data.
  • Scale features for distance-based and kernel methods (LOF, OneClassSVM).
  • Decide what you will do with a flag (alert, review, block) first; it determines the threshold more than any statistic.
  • Anomaly scores drift with the data. Re-check the detector regularly, as described in the lesson on saving models and deploying.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is an anomaly quick to isolate in an IsolationForest?

    Random splits quickly cut off points in sparse regions, so anomalies end up with short average path lengths.
  2. 2. What does contamination=0.05 do?

    Contamination only decides where to cut the ranking. On clean data it still flags about 5% of points.
  3. 3. When is LocalOutlierFactor better than a global method?

    LOF compares each point's local density with that of its neighbours, so it can find points that are near a dense cluster in absolute terms but far from it in relative terms.
  4. 4. Why is accuracy a poor way to evaluate an anomaly detector?

    With a tiny anomaly rate, the do-nothing detector scores very high accuracy. Use ranking metrics such as ROC AUC, average precision or precision at k.

Summary

  • Anomaly detectors rank points by "unusualness"; -1 labels come from a threshold.
  • IsolationForest, LOF, EllipticEnvelope and OneClassSVM make different assumptions: random isolation, local density, Gaussian shape and a kernel boundary.
  • contamination is a threshold guess, not a model parameter that finds the truth.
  • Use outlier mode to clean data you already have, novelty mode (novelty=True for LOF) to judge new data.
  • Without labels, evaluate with planted anomalies, expert review of the top scores and seed stability; with labels, use ROC AUC and average precision.

Next, applying matrix factorization to a very different problem: Recommendation Basics.