Explainable Isolation Forest

Machine Learning
Introduced by
Liu, F. T., Ting, K. M., & Zhou, Z.-H. (Isolation Forest); Lundberg, S. M. & Lee, S.-I. (SHAP explainability layer)
Year
2008 / 2017
Type
Anomaly detection with post-hoc explainability
Family
Machine learning

What does it do?

Explainable Isolation Forest combines the Isolation Forest anomaly detection algorithm with post-hoc explainability tools — most commonly SHAP (SHapley Additive exPlanations) — to not only flag anomalous observations but also reveal which features drove each anomaly score.

It bridges unsupervised anomaly detection with the interpretability demands of regulated and high-stakes domains.

The intuition behind the method

Isolation Forest detects anomalies by exploiting the fact that rare observations are isolated more quickly than normal ones when random partitions are made in the feature space. The anomaly score measures how few splits were needed to isolate a point: fewer splits imply greater abnormality. The score itself does not reveal which features caused an observation to be flagged. SHAP values attribute the model's output for a flagged record to individual features, relative to a baseline average over the reference data, so an analyst can see which variables pushed that record toward or away from the normal range. Because the anomaly score is a nonlinear transform of path length, these attributions describe the model's output, not an independent mechanism that produced the anomaly.

History

Isolation Forest was introduced by Liu, Ting, and Zhou at ICDM 2008 as an efficient, label-free anomaly detector that isolates anomalous points rather than profiling normal ones. The SHAP framework was presented by Lundberg and Lee in 2017 as a unified approach to explaining model predictions. Pairing the two is a post-hoc practice; the applications listed below describe possible uses rather than documented deployments.

When to use it

Use Explainable Isolation Forest when detecting anomalies in tabular data where each flagged case must be justified to a reviewer, for example in fraud detection, network intrusion monitoring, quality control, or clinical outlier identification. It suits unlabelled data, where no examples of known fraud or faults exist to train a supervised model, and features that are continuous or already numerically encoded. Do not use it when reliable, reasonably balanced labels are available and a supervised classifier would be more powerful, or when only an anomaly ranking is needed; standard Isolation Forest is then sufficient and cheaper, since it skips the attribution step.

Limitations and considerations

  • SHAP values describe the fitted model's score, not the true anomaly-generating mechanism, so explanation quality cannot exceed how well the forest fits the data.
  • The contamination rate only sets the flag cutoff; scores and SHAP values do not depend on it. A poorly chosen rate, however, makes the flagged set too large or too small, so the explanations are then attached to the wrong cases.
  • Anomalies that appear only in combinations of features may receive weaker scores. SHAP attributions would then reflect the individual-feature contributions to the score and may not reveal the joint pattern.
  • Computation grows with the number of trees and with the number of records explained; TreeSHAP over many records and large ensembles adds cost beyond fitting the forest.
  • Attributions are relative to a baseline set by the reference data, so values produced by different fitted models or different reference samples are not directly comparable.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Pairs unsupervised anomaly detection with per-record, feature-level attributions, so each flag comes with a breakdown of the variables that contributed to its score.
  • SHAP values are defined by the Shapley axioms (efficiency, symmetry, dummy, additivity); these hold for the function being explained, which is the chosen model output rather than the raw path length.
  • The original Isolation Forest work reports linear time complexity with a low constant and low memory requirements, and performs favourably against ORCA, LOF and random forests in terms of AUC and processing time, especially on large data sets. The attribution step adds cost on top of this (see limitations).
  • Requires no labels; the main choices are the contamination rate, the number of trees and the sub-sample size.
  • Per-record attributions give reviewers a concrete, contestable reason for each flag, which supports audit trails, although they explain the score rather than the underlying cause.
  • In the original empirical evaluation, the method also works well on high-dimensional problems with many irrelevant attributes, and in situations where the training set contains no anomalies.
  • Handles continuous features directly. Nominal categories must be one-hot encoded, and ordinal categories may be integer-coded only when their order is meaningful, because the isolation splits are numeric (see FAQ).

Applications

  • Fraud detection in financial transactions where each flagged case must be explained to investigators.
  • Network intrusion detection with feature-level attribution for security analysts.
  • Manufacturing quality control, identifying which sensor readings mark a defective unit.
  • Clinical outlier detection in electronic health records with explainability for clinicians.
  • Credit risk monitoring to flag unusual customer behaviour with interpretable reasons.
  • Data quality auditing to identify and explain anomalous records in research datasets.

How it works

How it works

1

An ensemble of isolation trees is grown on random subsamples of the data. Each tree recursively partitions the feature space by selecting a random feature and a random split value until every observation is isolated. The sub-sample size and the number of trees are the main settings to choose. No labels are needed; the algorithm is fully unsupervised.

2

Each observation receives an anomaly score between 0 and 1, computed from its average path length across all trees and normalised by c(n), the normalising constant defined for this score in Liu et al. (2008) and used in the score formula given in the mechanics section. Scores close to 1 indicate strong anomalies; scores near 0.5 indicate observations that are not clearly anomalous, which are usually read as typical.

Formula
s(x, n) = 2^{-E[h(x)] / c(n)}
3

A contamination threshold is applied, typically the expected proportion of anomalies in the dataset, to classify observations as anomalous or normal. This threshold can be set by domain knowledge or tuned via held-out validation.

4

TreeSHAP, a SHAP variant designed for tree-based models, can be applied to the fitted ensemble to estimate Shapley-style attributions for each observation. The output being explained must be chosen explicitly. Because the anomaly score is a non-linear function of path length, the attributions should be read as contributions to that chosen output relative to its baseline (expected) value over the reference data, not as exact additive parts of the anomaly score. Each feature receives a value indicating how it moved the output away from that baseline.

5

Feature-level SHAP values are visualised as waterfall plots, beeswarm plots, or bar charts. Analysts can rank which features are globally most responsible for anomalies and inspect individual cases to understand why a specific record was flagged, supporting audit trails and corrective action.

Common pitfalls

  • Setting contamination to an arbitrary default value without checking it against domain knowledge about how common anomalies are in the data.
  • Treating SHAP feature rankings as causal explanations — they indicate feature importance for the model's score, not necessarily the real-world cause of the anomaly.
  • Ignoring feature correlations when interpreting SHAP values: correlated features can share attribution in misleading ways.
  • Passing categorical codes into the forest as plain integers, since numeric splits then imply an order that the categories may not have (see FAQ).
  • Accepting the anomaly flags without human review in high-stakes contexts; a flagged record may be unusual but legitimate, and each false positive carries a cost.

Frequently asked questions

Do I need labeled anomaly data to train an Explainable Isolation Forest?

No. Isolation Forest is an unsupervised method and requires no labeled examples of anomalies or normal observations. Labels can be used afterward to evaluate flagging performance if available, but they are not needed for training.

How do I choose the contamination parameter?

Start from domain knowledge. If you expect roughly 2% fraud in your dataset, set contamination to 0.02. If no estimate is available, use held-out labeled examples to calibrate the threshold, or plot the score distribution and look for a natural break between scores.

Are SHAP values for Isolation Forest exact or approximate?

The attributions are Shapley-style values, computed with TreeSHAP for the tree ensemble, for the output function chosen for explanation. They are not an exact decomposition of the anomaly score s(x,n), because that score is a non-linear function of path length. The forest itself only approximates the true anomaly-generating process, and the attributions inherit that approximation.

Can Explainable Isolation Forest handle categorical features?

Isolation Forest operates on numeric splits, so categorical features must be numerically encoded before fitting. Nominal categories should be one-hot encoded; integer codes for ordinal categories are acceptable only when their order is meaningful. SHAP values can then be summed over the dummy columns that represent the same original variable to give a combined feature attribution.

When should I prefer a supervised classifier over Explainable Isolation Forest?

If you have reliable, reasonably balanced labeled examples of anomalies, a supervised classifier with SHAP explanations is usually the stronger choice. Reserve Explainable Isolation Forest for settings where labels are absent, rare, or unreliable.

How do I check the flags when no labels are available?

Refit the model with several random seeds and check that the flagged set and the top-ranked SHAP features remain broadly similar. Repeat the analysis across a plausible range of contamination rates to see how sensitive the flags are. Then have a domain expert review a random sample of flagged records together with some unflagged records close to the cutoff. Any labels that become available later can be used to measure flagging performance.

Related methods

Sources

  1. Lundberg, S. M., & Lee, S.-I. (2017). A unified approach to interpreting model predictions. Advances in Neural Information Processing Systems, 30, 4765–4774.

    Open publication reference
  2. Liu, F. T., Ting, K. M., & Zhou, Z.-H. (2008). Isolation forest. In Proceedings of the 8th IEEE International Conference on Data Mining (ICDM 2008), pp. 413–422. IEEE.

    Open DOI reference