Unsupervised anomaly detection
Flag unusual records when labeled anomalies are unavailable.
Scikit-Learn · Class
IsolationForest detects outliers by randomly partitioning feature space into trees. Observations isolated with shorter average paths are treated as more anomalous.
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.
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)| Name | Type | Description |
|---|---|---|
n_estimators | int | Number of trees in the ensemble; default 100. |
max_samples | int, float, or 'auto' | Number of samples drawn for each tree; default 'auto'. |
contamination | float or 'auto' | Expected proportion of outliers used to set the score threshold; default 'auto'. |
max_features | int or float | Number or fraction of features considered by each tree; default 1.0. |
bootstrap | bool | Whether samples are drawn with replacement; default False. |
n_jobs | int or None | Number of jobs used for fitting and predicting; default None. |
random_state | int, RandomState instance, or None | Controls pseudorandomness; default None. |
verbose | int | Verbosity level; default 0. |
warm_start | bool | Whether fitting again adds trees to the existing forest; default False. |
A fitted IsolationForest instance from fit; predict returns labels identifying inliers and outliers, while decision_function returns anomaly scores.
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: inlierFits an isolation forest and predicts whether each observation is an inlier or outlier.
Flag unusual records when labeled anomalies are unavailable.
Screen multivariate measurements for observations far from the dominant pattern.
Grow a fitted forest incrementally as more compute or trees are desired.
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.
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.
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.IsolationForestExtraTreeRegressorensemble.IsolationForestoutlier detection