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.
Example
forest := IsolationForest->New(100, 0, 0.05, 42);
forest->Fit(X);
scores := forest->Score(X_test);Operations
- New
- AveragePathLength
- DecisionFunction
- Fit
- FitPredict
- GetError
- GetSampleSize
- GetSensitivity
- GetSensitivityName
- GetThreshold
- GetTrainingScores
- GetTreeCount
- IsFitted
- PathLengths
- Predict
- Score
- SetSensitivity
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) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| n | Int | node count |
Return
| Type | Description |
|---|---|
| Float | c(n) |
DecisionFunction #
Score minus the threshold; positive means outlier.
method : public : DecisionFunction(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | values, 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[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | true on success |
FitPredict #
Fits, then labels the training rows.
method : public : FitPredict(X:Float[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | labels (true = outlier), or Nil when the fit failed |
GetError #
Why the last call failed.
method : public : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | the reason, or an empty string |
GetSampleSize #
Rows each tree was grown on (max_samples after the default and the cap).
method : public : GetSampleSize() ~ IntReturn
| Type | Description |
|---|---|
| Int | sample size, or 0 when unfitted |
GetSensitivity #
The contamination (0.0 = auto).
method : public : GetSensitivity() ~ FloatReturn
| Type | Description |
|---|---|
| Float | contamination |
GetSensitivityName #
The setting's name.
method : public : GetSensitivityName() ~ StringReturn
| Type | Description |
|---|---|
| String | "contamination" |
GetThreshold #
The score threshold (sklearn's -offset_).
method : public : GetThreshold() ~ FloatReturn
| Type | Description |
|---|---|
| Float | threshold |
GetTrainingScores #
The training rows' scores.
method : public : GetTrainingScores() ~ Float[]Return
| Type | Description |
|---|---|
| Float | scores, or Nil when unfitted |
GetTreeCount #
Trees in the forest.
method : public : GetTreeCount() ~ IntReturn
| Type | Description |
|---|---|
| Int | tree count, or 0 when unfitted |
IsFitted #
Whether the forest is grown.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true 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
| Name | Type | Description |
|---|---|---|
| num_trees | Int | trees (n_estimators), >= 1 |
| max_samples | Int | rows per tree; 0 means min(256, n); capped at n |
| contamination | Float | expected outlier fraction in (0, 0.5], or 0.0 for "auto" |
| seed | Int | random seed |
PathLengths #
Mean path length E[h(x)] of each row over the trees.
method : public : PathLengths(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | mean 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
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | labels, 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
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | scores, 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) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | Float | contamination in (0, 0.5], or 0.0 for auto |
Return
| Type | Description |
|---|---|
| Bool | false when out of range |