Skip to Content
OchreML
Trees, Ensembles & Boosting

IsolationForest

Unsupervised tree-based anomaly detection (Liu et al., 2008) backed by Rust Rayon parallelism.

IsolationForest is an unsupervised anomaly detection ensemble (Liu et al., 2008) that isolates observations by randomly selecting a feature and a random split threshold between the feature's minimum and maximum values. Anomalies require shorter average path lengths to isolate and receive lower anomaly scores. (^▽^)

Backend & Hardware Acceleration

  • CPU Rayon Parallelism: Isolation trees are built and scored in parallel across CPU cores via Rust Rayon with deterministic per-tree RNG seeding when random_state is provided.
  • Accelerator Fallback: Accepts any OchreML device string ("auto", "cpu", "cuda:0", "wgpu", "tpu") for consistent pipeline configuration and runs branch-heavy random isolation trees on CPU Rayon.
  • Zero-Copy Buffers: fit, score_samples, decision_function, and predict pass contiguous float64 arrays directly into Rust; predict returns int64 labels (1 for inliers, -1 for outliers).

Import & Signature

from ochreml.ensemble import IsolationForest
# or: from ochreml import IsolationForest

IsolationForest(
    n_estimators: int = 100,
    max_samples: Union[str, int, float] = "auto",
    contamination: Union[str, float] = "auto",
    max_features: Union[int, float] = 1.0,
    bootstrap: bool = False,
    random_state: Optional[int] = None,
    device: Optional[str] = None,
)

Parameters

ParameterTypeDefaultDescription
n_estimatorsint100Number of base isolation trees in the ensemble.
max_samples"auto" | int | float"auto"Number of samples drawn from X to train each tree ("auto" uses min(256, n_samples), positive int, or fraction float in (0.0, 1.0]).
contamination"auto" | float"auto"Expected proportion of outliers in (0.0, 0.5] used to calibrate offset_, or "auto" for the Liu et al. default threshold (-0.5).
max_featuresint | float1.0Number (positive int <= n_features) or fraction (float in (0.0, 1.0]) of features drawn per tree.
bootstrapboolFalseIf True, individual trees are fit on subsets sampled with replacement.
random_stateint | NoneNoneRandom seed controlling feature, subsample, and split threshold selection.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
max_samples_int | NoneActual number of samples used to train each isolation tree.
offset_float | NoneDecision threshold offset applied to score_samples(X) to compute decision_function(X).
n_features_in_int | NoneNumber of features seen during fit.
device_str | NoneResolved hardware device string used by the underlying Rust estimator.

Methods

MethodSignatureDescription
fitfit(X, y=None) -> IsolationForestFit the unsupervised isolation forest on X (y is ignored for API compatibility).
score_samplesscore_samples(X) -> np.ndarrayOpposite of the Liu et al. (2008) anomaly score; lower (more negative) values indicate stronger anomalies.
decision_functiondecision_function(X) -> np.ndarrayShifted anomaly score score_samples(X) - offset_; negative values indicate outliers and positive values indicate inliers.
predictpredict(X) -> np.ndarrayReturn int64 labels: 1 for inliers and -1 for outliers.
savesave(path: str) -> NoneSave the fitted forest to a binary .dit file.
loadIsolationForest.load(path: str, device: str | None = None)Load a fitted IsolationForest from a .dit file.
toto(device: str) -> IsolationForestMove the estimator compute target to device.
set_deviceset_device(device: str | None = None) -> IsolationForestSet or reset the compute device for the estimator.

Example

import numpy as np
from ochreml.ensemble import IsolationForest

rng = np.random.default_rng(42)
X_inliers = rng.normal(loc=0.0, scale=0.4, size=(100, 2))
X_outliers = np.array([[4.0, 4.0], [-4.5, 3.5], [3.8, -4.2]], dtype=np.float64)
X_train = np.vstack([X_inliers, X_outliers])

iso = IsolationForest(
    n_estimators=100,
    contamination=0.05,
    random_state=42,
    device="auto",
)
iso.fit(X_train)

preds = iso.predict(X_outliers)
scores = iso.decision_function(X_outliers)
print("Outlier predictions (expect -1):", preds)
print("Outlier decision scores:", scores)
print("Effective max_samples_:", iso.max_samples_)

On this page