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_stateis provided. - Accelerator Fallback: Accepts any OchreML
devicestring ("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, andpredictpass contiguousfloat64arrays directly into Rust;predictreturnsint64labels (1for inliers,-1for 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
| Parameter | Type | Default | Description |
|---|---|---|---|
n_estimators | int | 100 | Number 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_features | int | float | 1.0 | Number (positive int <= n_features) or fraction (float in (0.0, 1.0]) of features drawn per tree. |
bootstrap | bool | False | If True, individual trees are fit on subsets sampled with replacement. |
random_state | int | None | None | Random seed controlling feature, subsample, and split threshold selection. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
max_samples_ | int | None | Actual number of samples used to train each isolation tree. |
offset_ | float | None | Decision threshold offset applied to score_samples(X) to compute decision_function(X). |
n_features_in_ | int | None | Number of features seen during fit. |
device_ | str | None | Resolved hardware device string used by the underlying Rust estimator. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y=None) -> IsolationForest | Fit the unsupervised isolation forest on X (y is ignored for API compatibility). |
score_samples | score_samples(X) -> np.ndarray | Opposite of the Liu et al. (2008) anomaly score; lower (more negative) values indicate stronger anomalies. |
decision_function | decision_function(X) -> np.ndarray | Shifted anomaly score score_samples(X) - offset_; negative values indicate outliers and positive values indicate inliers. |
predict | predict(X) -> np.ndarray | Return int64 labels: 1 for inliers and -1 for outliers. |
save | save(path: str) -> None | Save the fitted forest to a binary .dit file. |
load | IsolationForest.load(path: str, device: str | None = None) | Load a fitted IsolationForest from a .dit file. |
to | to(device: str) -> IsolationForest | Move the estimator compute target to device. |
set_device | set_device(device: str | None = None) -> IsolationForest | Set 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_)