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
- New
- GetAccuracy
- GetF1
- GetFalseNegatives
- GetFalsePositiveRate
- GetFalsePositives
- GetFlagged
- GetPrecision
- GetRecall
- GetSpecificity
- GetTotal
- GetTrueNegatives
- GetTruePositives
- IsValid
- ToMatrix
- ToString
GetAccuracy #
(TP + TN) / total.
method : public : GetAccuracy() ~ FloatReturn
| Type | Description |
|---|---|
| Float | accuracy, or 0.0 for an empty sample |
GetF1 #
2 TP / (2 TP + FP + FN), the harmonic mean of precision and recall.
method : public : GetF1() ~ FloatReturn
| Type | Description |
|---|---|
| Float | F1, or 0.0 when TP, FP and FN are all zero |
GetFalseNegatives #
Unflagged rows that really are positive.
method : public : GetFalseNegatives() ~ IntReturn
| Type | Description |
|---|---|
| Int | false negatives |
GetFalsePositiveRate #
FP / (FP + TN): of the real negatives, the share wrongly flagged (false-alarm rate).
method : public : GetFalsePositiveRate() ~ FloatReturn
| Type | Description |
|---|---|
| Float | the false-positive rate, or 0.0 when there are no real negatives |
GetFalsePositives #
Flagged rows that are really negative.
method : public : GetFalsePositives() ~ IntReturn
| Type | Description |
|---|---|
| Int | false positives |
GetFlagged #
Rows flagged positive.
method : public : GetFlagged() ~ IntReturn
| Type | Description |
|---|---|
| Int | TP + FP |
GetPrecision #
TP / (TP + FP): of the rows flagged, the share that were real.
method : public : GetPrecision() ~ FloatReturn
| Type | Description |
|---|---|
| Float | precision, 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() ~ FloatReturn
| Type | Description |
|---|---|
| Float | recall, 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() ~ FloatReturn
| Type | Description |
|---|---|
| Float | specificity, or 0.0 when there are no real negatives |
GetTrueNegatives #
Unflagged rows that are really negative.
method : public : GetTrueNegatives() ~ IntReturn
| Type | Description |
|---|---|
| Int | true negatives |
GetTruePositives #
Flagged rows that really are positive.
method : public : GetTruePositives() ~ IntReturn
| Type | Description |
|---|---|
| Int | true positives |
IsValid #
False when the inputs were Nil, of different lengths, or the counts negative; every count is then 0.
method : public : IsValid() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true when the counts are meaningful |
New # constructor
Counts predictions against actuals.
New(predictions:Bool[], actuals:Bool[])Parameters
| Name | Type | Description |
|---|---|---|
| predictions | Bool | predicted labels, true = positive (flagged) |
| actuals | Bool | true labels, true = positive (anomaly) |
New # constructor
From counts.
New(tp:Int, fp:Int, tn:Int, fn:Int)Parameters
| Name | Type | Description |
|---|---|---|
| tp | Int | true positives |
| fp | Int | false positives |
| tn | Int | true negatives |
| fn | Int | false negatives |