v2026.11.2
All Bundles
Bundle

Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

IsolationForest

Isolation Forest (Liu, Ting and Zhou 2008), as scikit-learn implements it.

Each tree isolates a random subsample of max_samples rows (default min(256, n), drawn without replacement) by recursive random splits: a feature drawn among those that vary in the node, a split point uniform between its min and max. Trees stop at depth ceil(log2(max_samples)). An anomaly isolates quickly, so its average path length E[h(x)] is short. The score is 2^(-E[h(x)] / c(max_samples)), in (0, 1], where c(n) is the average path length of an unsuccessful binary-search -tree search; h adds c(leaf size) at a leaf. That is the negation of sklearn's score_samples.

Contamination 0.0 is sklearn's "auto": threshold 0.5 (offset_ -0.5). Otherwise the threshold is the (1 - contamination) quantile of the training scores. Contamination does not change the trees, so SetSensitivity keeps the forest.

Implements: OutlierDetector

Example

forest := IsolationForest->New(100, 0, 0.05, 42);
forest->Fit(X);
scores := forest->Score(X_test);

Operations

AveragePathLength # function

The average path length of an unsuccessful search in a binary search tree of n nodes: c(n) = 2 H(n-1) - 2(n-1)/n with H(i) = ln(i) + 0.5772156649, c(1) = 0 (and c(0) = 0), c(2) = 1. scikit-learn's _average_path_length.

function : AveragePathLength(n:Int) ~ Float

Parameters

NameTypeDescription
nIntnode count

Return

TypeDescription
Floatc(n)

DecisionFunction #

Score minus the threshold; positive means outlier.

method : public : DecisionFunction(X:Float[,]) ~ Float[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatvalues, or Nil when unfitted or the input is refused

Fit #

Grows the forest and sets the threshold from the training scores.

method : public : Fit(X:Float[,]) ~ Bool

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Booltrue on success

FitPredict #

Fits, then labels the training rows.

method : public : FitPredict(X:Float[,]) ~ Bool[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Boollabels (true = outlier), or Nil when the fit failed

GetError #

Why the last call failed.

method : public : GetError() ~ String

Return

TypeDescription
Stringthe reason, or an empty string

GetSampleSize #

Rows each tree was grown on (max_samples after the default and the cap).

method : public : GetSampleSize() ~ Int

Return

TypeDescription
Intsample size, or 0 when unfitted

GetSensitivity #

The contamination (0.0 = auto).

method : public : GetSensitivity() ~ Float

Return

TypeDescription
Floatcontamination

GetSensitivityName #

The setting's name.

method : public : GetSensitivityName() ~ String

Return

TypeDescription
String"contamination"

GetThreshold #

The score threshold (sklearn's -offset_).

method : public : GetThreshold() ~ Float

Return

TypeDescription
Floatthreshold

GetTrainingScores #

The training rows' scores.

method : public : GetTrainingScores() ~ Float[]

Return

TypeDescription
Floatscores, or Nil when unfitted

GetTreeCount #

Trees in the forest.

method : public : GetTreeCount() ~ Int

Return

TypeDescription
Inttree count, or 0 when unfitted

IsFitted #

Whether the forest is grown.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue when fitted

New # constructor

Constructor with scikit-learn's defaults: 100 trees, max_samples min(256, n), contamination auto, seed 42.

New()

New # constructor

Constructor.

New(num_trees:Int, max_samples:Int, contamination:Float, seed:Int)

Parameters

NameTypeDescription
num_treesInttrees (n_estimators), >= 1
max_samplesIntrows per tree; 0 means min(256, n); capped at n
contaminationFloatexpected outlier fraction in (0, 0.5], or 0.0 for "auto"
seedIntrandom seed

PathLengths #

Mean path length E[h(x)] of each row over the trees.

method : public : PathLengths(X:Float[,]) ~ Float[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatmean path lengths, or Nil when unfitted or the input is refused

Predict #

Labels rows: true where the score exceeds the threshold.

method : public : Predict(X:Float[,]) ~ Bool[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Boollabels, or Nil when unfitted or the input is refused

Score #

Anomaly scores 2^(-E[h(x)]/c(max_samples)), in (0, 1].

method : public : Score(X:Float[,]) ~ Float[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatscores, or Nil when unfitted or the input is refused

SetSensitivity #

Sets the contamination; a fitted forest keeps its trees and re-derives the threshold from its training scores.

method : public : SetSensitivity(value:Float) ~ Bool

Parameters

NameTypeDescription
valueFloatcontamination in (0, 0.5], or 0.0 for auto

Return

TypeDescription
Boolfalse when out of range