Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

IsolationForest

IsolationForest detects outliers by randomly partitioning feature space into trees. Observations isolated with shorter average paths are treated as more anomalous.

How it works

IsolationForest builds randomized trees by choosing a feature and a split value, then measures how many splits isolate each observation. Samples isolated in fewer splits receive stronger anomaly evidence; the forest combines tree-level scores to make predictions.

Syntax

class sklearn.ensemble.IsolationForest(*, n_estimators=100, max_samples='auto', contamination='auto', max_features=1.0, bootstrap=False, n_jobs=None, random_state=None, verbose=0, warm_start=False)

Parameters

NameTypeDescription
n_estimatorsintNumber of trees in the ensemble; default 100.
max_samplesint, float, or 'auto'Number of samples drawn for each tree; default 'auto'.
contaminationfloat or 'auto'Expected proportion of outliers used to set the score threshold; default 'auto'.
max_featuresint or floatNumber or fraction of features considered by each tree; default 1.0.
bootstrapboolWhether samples are drawn with replacement; default False.
n_jobsint or NoneNumber of jobs used for fitting and predicting; default None.
random_stateint, RandomState instance, or NoneControls pseudorandomness; default None.
verboseintVerbosity level; default 0.
warm_startboolWhether fitting again adds trees to the existing forest; default False.

Returns

A fitted IsolationForest instance from fit; predict returns labels identifying inliers and outliers, while decision_function returns anomaly scores.

Key points

  • Each isolation tree chooses a feature at random, then chooses a split within that feature's observed range.
  • The estimator averages isolation path lengths across trees to quantify how typical an observation is.
  • Outliers tend to be separated from other observations with fewer random partitions.
  • A sample's forest-level anomaly score is the mean of its scores from the individual trees.
  • n_estimators sets how many underlying tree estimators the forest contains.
  • max_samples determines the training-sample draw used by each tree.

Common mistakes

  • When max_samples exceeds the dataset size, the estimator uses the complete dataset for every tree.

Example: Detect outliers with IsolationForest

from sklearn.ensemble import IsolationForest
import numpy as np

X = np.array([[0.0, 0.0], [0.2, -0.1], [-0.1, 0.1], [8.0, 8.0]])
model = IsolationForest(contamination="auto", random_state=0)
model.fit(X)
print(model.predict(X))  # -1: outlier, 1: inlier

Fits an isolation forest and predicts whether each observation is an inlier or outlier.

When to use it

Unsupervised anomaly detection

Flag unusual records when labeled anomalies are unavailable.

Outlier screening

Screen multivariate measurements for observations far from the dominant pattern.

Incremental ensemble expansion

Grow a fitted forest incrementally as more compute or trees are desired.

Interview questions

QHow does IsolationForest detect anomalies?

IsolationForest isolates points through random feature selection and random split values. Anomalies usually require fewer splits to isolate, so shorter average paths indicate less-normal observations.

Follow-up: How does the forest combine trees? It aggregates the anomaly score contributed by each tree, using the mean score across the forest.

QWhat do n_estimators and max_samples control?

n_estimators controls the number of base estimators in the ensemble. max_samples controls how many training samples are drawn for each estimator, with the automatic setting selecting an appropriate sample count.

Follow-up: What happens when max_samples is larger than the dataset? Every tree uses all available samples rather than sampling a larger set.

QHow are predicted outliers determined?

contamination specifies the expected proportion of outliers and is used to establish the decision threshold. The default 'auto' setting derives the threshold from the scoring method rather than requiring an explicit proportion.

Follow-up: Can the forest be expanded after fitting? Yes: set warm_start=True, raise n_estimators, and fit again to add trees.

  • sklearn.ensemble.IsolationForest
  • ExtraTreeRegressor
  • ensemble.IsolationForest
  • outlier detection

Source: IsolationForest. Checked 2026-10-15.