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.

LocalOutlierFactor

Local Outlier Factor (Breunig et al. 2000), as scikit-learn implements it.

A row's local reachability density is the inverse of its mean reachability distance to its k nearest neighbours, reach(a, o) = max(k-distance(o), d(a, o)); its LOF is the mean density of those neighbours over its own. About 1 inside a cluster, well above 1 for a row in a sparser region than its neighbours. The score is the LOF (sklearn's score_samples is -LOF).

k defaults to 20 and is capped at n - 1. Neighbours come from the exact KDTree; a training row's neighbours exclude itself. Contamination 0.0 is sklearn's "auto": threshold 1.5. Otherwise the threshold is the (1 - contamination) quantile of the training LOFs.

Without novelty (the default) the detector labels its training rows only, through FitPredict and GetTrainingScores; Predict and Score refuse, as sklearn raises, because scoring a training row as new would count it as its own neighbour. With novelty, new rows are scored against the training graph.

Implements: OutlierDetector

Example

lof := LocalOutlierFactor->New(20, 0.05, false);
flags := lof->FitPredict(X);

Operations

DecisionFunction #

Score minus the threshold; positive means outlier (novelty mode only).

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

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatvalues, or Nil

Fit #

Builds the neighbour graph and the training LOFs.

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

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Booltrue on success

FitPredict #

Fits, then labels the training rows by their LOF.

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

Parameters

NameTypeDescription
XFloatrows x features

Return

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

GetDensities #

The training rows' local reachability densities.

method : public : GetDensities() ~ Float[]

Return

TypeDescription
Floatdensities, or Nil when unfitted

GetError #

Why the last call failed.

method : public : GetError() ~ String

Return

TypeDescription
Stringthe reason, or an empty string

GetNeighbors #

The neighbour count in use: k, capped at n - 1.

method : public : GetNeighbors() ~ Int

Return

TypeDescription
Intneighbours, or the requested k 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 LOF threshold (sklearn's -offset_).

method : public : GetThreshold() ~ Float

Return

TypeDescription
Floatthreshold

GetTrainingScores #

The training rows' LOFs (sklearn's -negative_outlier_factor_).

method : public : GetTrainingScores() ~ Float[]

Return

TypeDescription
FloatLOFs, or Nil when unfitted

IsFitted #

Whether the detector is fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue when fitted

New # constructor

Constructor with scikit-learn's defaults: k 20, contamination auto, no novelty.

New()

New # constructor

Constructor.

New(k:Int, contamination:Float, novelty:Bool)

Parameters

NameTypeDescription
kIntneighbours (n_neighbors), >= 1
contaminationFloatexpected outlier fraction in (0, 0.5], or 0.0 for auto
noveltyBooltrue to score new rows with Predict and Score

Predict #

Labels new rows (novelty mode only).

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

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Boollabels, or Nil when unfitted, not in novelty mode, or the input is refused

Score #

LOF of new rows against the training data (novelty mode only).

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

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatscores, or Nil when unfitted, not in novelty mode, or the input is refused

SetSensitivity #

Sets the contamination; a fitted detector keeps its neighbour graph and re-derives the threshold from the training LOFs.

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