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.

ConfusionMatrix

The four counts of a binary decision -- true positives, false positives, true negatives, false negatives -- and every rate read off them.

For outlier detection the anomaly is the positive class: a prediction of true (flagged) on a row that really is an anomaly is a true positive. Metrics' Precision, Recall and F1Score compute the same rates one at a time; this keeps them together with the counts they rest on, which is what a sweep table needs.

Every rate guards its denominator and returns 0.0 when it is zero, as scikit-learn does with zero_division=0: precision when nothing was flagged, recall when there are no real positives, the false-positive rate when there are no real negatives, F1 when there are no true positives, false positives or false negatives, accuracy on an empty sample.

Example

cm := ConfusionMatrix->New(flags, labels);
"precision={$cm->GetPrecision()} recall={$cm->GetRecall()} f1={$cm->GetF1()} fpr={$cm->GetFalsePositiveRate()}"->PrintLine();

Operations

GetAccuracy #

(TP + TN) / total.

method : public : GetAccuracy() ~ Float

Return

TypeDescription
Floataccuracy, or 0.0 for an empty sample

GetF1 #

2 TP / (2 TP + FP + FN), the harmonic mean of precision and recall.

method : public : GetF1() ~ Float

Return

TypeDescription
FloatF1, or 0.0 when TP, FP and FN are all zero

GetFalseNegatives #

Unflagged rows that really are positive.

method : public : GetFalseNegatives() ~ Int

Return

TypeDescription
Intfalse negatives

GetFalsePositiveRate #

FP / (FP + TN): of the real negatives, the share wrongly flagged (false-alarm rate).

method : public : GetFalsePositiveRate() ~ Float

Return

TypeDescription
Floatthe false-positive rate, or 0.0 when there are no real negatives

GetFalsePositives #

Flagged rows that are really negative.

method : public : GetFalsePositives() ~ Int

Return

TypeDescription
Intfalse positives

GetFlagged #

Rows flagged positive.

method : public : GetFlagged() ~ Int

Return

TypeDescription
IntTP + FP

GetPrecision #

TP / (TP + FP): of the rows flagged, the share that were real.

method : public : GetPrecision() ~ Float

Return

TypeDescription
Floatprecision, or 0.0 when nothing was flagged

GetRecall #

TP / (TP + FN): of the real positives, the share flagged (true-positive rate, sensitivity, detection rate).

method : public : GetRecall() ~ Float

Return

TypeDescription
Floatrecall, or 0.0 when there are no real positives

GetSpecificity #

TN / (TN + FP), the true-negative rate: 1 minus the false-positive rate.

method : public : GetSpecificity() ~ Float

Return

TypeDescription
Floatspecificity, or 0.0 when there are no real negatives

GetTotal #

Rows counted.

method : public : GetTotal() ~ Int

Return

TypeDescription
IntTP + FP + TN + FN

GetTrueNegatives #

Unflagged rows that are really negative.

method : public : GetTrueNegatives() ~ Int

Return

TypeDescription
Inttrue negatives

GetTruePositives #

Flagged rows that really are positive.

method : public : GetTruePositives() ~ Int

Return

TypeDescription
Inttrue positives

IsValid #

False when the inputs were Nil, of different lengths, or the counts negative; every count is then 0.

method : public : IsValid() ~ Bool

Return

TypeDescription
Booltrue when the counts are meaningful

New # constructor

Counts predictions against actuals.

New(predictions:Bool[], actuals:Bool[])

Parameters

NameTypeDescription
predictionsBoolpredicted labels, true = positive (flagged)
actualsBooltrue labels, true = positive (anomaly)

New # constructor

From counts.

New(tp:Int, fp:Int, tn:Int, fn:Int)

Parameters

NameTypeDescription
tpInttrue positives
fpIntfalse positives
tnInttrue negatives
fnIntfalse negatives

ToMatrix #

The counts in Metrics->ConfusionMatrix's layout: [[TN, FP], [FN, TP]].

method : public : ToMatrix() ~ Int[,]

Return

TypeDescription
Int2 x 2 counts

ToString #

The counts and rates on one line.

method : public : ToString() ~ String

Return

TypeDescription
Stringsummary