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.
OutlierDetector
An unsupervised anomaly detector: fitted on rows, it labels and scores rows.
Conventions, shared by every detector so they are interchangeable (see docs/ML_OUTLIERS_DESIGN.md):
- Labels are Bool and true means outlier. scikit-learn uses +1 for inliers and -1 for outliers, so sklearn's -1 is our true. The anomaly is the positive class, so a prediction feeds Metrics and ConfusionMatrix directly. - Scores: higher means more anomalous, so they go straight into Metrics->AucRoc and RecallAtFpr. Where scikit-learn has score_samples (higher means more normal), Score is exactly its negation. - GetThreshold is the cut on Score, sklearn's -offset_. DecisionFunction is Score minus the threshold, so positive means outlier (sklearn's -decision_function). A row is an outlier when its score is strictly greater than the threshold, as in sklearn's predict. - Where a detector takes a contamination, the threshold is that quantile of the training scores (numpy's linear interpolation), as in scikit-learn. - NaN input is refused: Fit returns false and GetError says where; Predict and Score return Nil.
Example
detector : OutlierDetector := IsolationForest->New();
if(detector->Fit(X)) {
flags := detector->Predict(X_new); # true = outlier
scores := detector->Score(X_new); # higher = more anomalous
auc := Metrics->AucRoc(scores, y_new);
};Operations
- DecisionFunction
- Fit
- FitPredict
- GetError
- GetSensitivity
- GetSensitivityName
- GetThreshold
- GetTrainingScores
- IsFitted
- Predict
- Score
- SetSensitivity
DecisionFunction # virtual
Score minus the threshold: positive means outlier, the negation of scikit-learn's decision_function.
method : public : virtual : DecisionFunction(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | one value per row, or Nil when unfitted or the input is refused |
Fit # virtual
Fits the detector to training rows.
method : public : virtual : Fit(X:Float[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | true on success; on false GetError says why |
FitPredict # virtual
Fits, then labels the training rows themselves. For LocalOutlierFactor without novelty this is the only way to label (each training row is scored without counting itself as a neighbour); for DbscanDetector it is DBSCAN's noise.
method : public : virtual : FitPredict(X:Float[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | one label per row (true = outlier), or Nil when the fit failed |
GetError # virtual
Why the last Fit, Predict or Score failed.
method : public : virtual : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | the reason, or an empty string |
GetSensitivity # virtual
The current sensitivity setting.
method : public : virtual : GetSensitivity() ~ FloatReturn
| Type | Description |
|---|---|
| Float | the setting |
GetSensitivityName # virtual
The name of the sensitivity setting, e.g. "contamination".
method : public : virtual : GetSensitivityName() ~ StringReturn
| Type | Description |
|---|---|
| String | setting name |
GetThreshold # virtual
The cut on Score above which a row is an outlier (scikit-learn's -offset_).
method : public : virtual : GetThreshold() ~ FloatReturn
| Type | Description |
|---|---|
| Float | the threshold |
GetTrainingScores # virtual
The scores of the training rows the threshold was taken from.
method : public : virtual : GetTrainingScores() ~ Float[]Return
| Type | Description |
|---|---|
| Float | one score per training row, or Nil when unfitted |
IsFitted # virtual
Whether the detector is fitted and ready to label.
method : public : virtual : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true when fitted |
Predict # virtual
Labels new rows: true where the score is strictly above the threshold.
method : public : virtual : Predict(X:Float[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features, as many columns as the training data |
Return
| Type | Description |
|---|---|
| Bool | one label per row (true = outlier), or Nil when unfitted or the input is refused |
Score # virtual
Anomaly scores for new rows; higher means more anomalous.
method : public : virtual : Score(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features, as many columns as the training data |
Return
| Type | Description |
|---|---|
| Float | one score per row, or Nil when unfitted or the input is refused |
SetSensitivity # virtual
Sets the detector's sensitivity setting (GetSensitivityName names it). When the setting only moves the threshold (a z-score threshold, a contamination, a percentile), a fitted detector re-derives its threshold from its training scores and stays fitted; when it changes the model (DBSCAN's eps) the detector becomes unfitted and must be fitted again.
method : public : virtual : SetSensitivity(value:Float) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | Float | the new setting |
Return
| Type | Description |
|---|---|
| Bool | false when the value is out of range (nothing is changed) |