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.
Example
lof := LocalOutlierFactor->New(20, 0.05, false);
flags := lof->FitPredict(X);Operations
- New
- DecisionFunction
- Fit
- FitPredict
- GetDensities
- GetError
- GetNeighbors
- GetSensitivity
- GetSensitivityName
- GetThreshold
- GetTrainingScores
- IsFitted
- Predict
- Score
- SetSensitivity
DecisionFunction #
Score minus the threshold; positive means outlier (novelty mode only).
method : public : DecisionFunction(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | values, or Nil |
Fit #
Builds the neighbour graph and the training LOFs.
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 by their LOF.
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 |
GetDensities #
The training rows' local reachability densities.
method : public : GetDensities() ~ Float[]Return
| Type | Description |
|---|---|
| Float | densities, or Nil when unfitted |
GetError #
Why the last call failed.
method : public : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | the reason, or an empty string |
GetNeighbors #
The neighbour count in use: k, capped at n - 1.
method : public : GetNeighbors() ~ IntReturn
| Type | Description |
|---|---|
| Int | neighbours, or the requested k 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 LOF threshold (sklearn's -offset_).
method : public : GetThreshold() ~ FloatReturn
| Type | Description |
|---|---|
| Float | threshold |
GetTrainingScores #
The training rows' LOFs (sklearn's -negative_outlier_factor_).
method : public : GetTrainingScores() ~ Float[]Return
| Type | Description |
|---|---|
| Float | LOFs, or Nil when unfitted |
IsFitted #
Whether the detector is fitted.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true 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
| Name | Type | Description |
|---|---|---|
| k | Int | neighbours (n_neighbors), >= 1 |
| contamination | Float | expected outlier fraction in (0, 0.5], or 0.0 for auto |
| novelty | Bool | true to score new rows with Predict and Score |
Predict #
Labels new rows (novelty mode only).
method : public : Predict(X:Float[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | labels, 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
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | scores, 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) ~ 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 |