Metrics multilabelconfusionmatrix compute - CyrilB1531/lodestar GitHub Wiki
Development build. This page describes
main, not a released package. The latest published Lodestar.Metrics is 0.3.0 — read its documentation.
Home › Metrics › Classification metrics
One 2×2 matrix per label or per sample — sklearn.metrics.multilabel_confusion_matrix.
public static ConfusionMatrix[] Compute(ReadOnlySpan<int> yTrue, ReadOnlySpan<int> yPred, ReadOnlySpan<int> labels = default, ReadOnlySpan<double> sampleWeight = default)
public static ConfusionMatrix[] Compute(ReadOnlySpan<bool> yTrue, ReadOnlySpan<bool> yPred, int labelCount, bool samplewise = false, ReadOnlySpan<double> sampleWeight = default)Parameters — the first overload takes yTrue and yPred as one label per sample and reports one
matrix per class, with labels fixing which classes and in what order; omit it for the sorted union
of both inputs. The second takes them as a row-major label matrix of labelCount values per row, and
samplewise decides whether to count one matrix per label or one per row. sampleWeight is one
weight per sample — per row, not per label.
Returns — a fresh ConfusionMatrix[]: one per class in label order, one per label in column
order, or one per sample in row order.
Exceptions — ArgumentException when the inputs disagree in length, are empty, when the matrix
is not a whole number of rows of labelCount, or when the weights do not match the sample count. A sampleWeight holding NaN or an infinity is refused with "Input sample_weight contains NaN." or its infinity counterpart, and one that is zero throughout with "Sample weights must contain at least one non-zero number." — both ArgumentException naming sampleWeight, as scikit-learn's _check_sample_weight refuses them. Weights that merely sum to zero are counted, as the reference counts them.
Example — three labels over two samples.
using Lodestar.Metrics;
bool[] truth = [true, false, true, false, true, true];
bool[] predicted = [true, false, false, true, true, true];
ConfusionMatrix[] perLabel = MultilabelConfusionMatrix.Compute(truth, predicted, labelCount: 3);
int matrices = perLabel.Length; // => 3Counting the same input by sample instead returns one matrix per row:
using Lodestar.Metrics;
bool[] truth = [true, false, true, false, true, true];
bool[] predicted = [true, false, false, true, true, true];
ConfusionMatrix[] perSample = MultilabelConfusionMatrix.Compute(truth, predicted, 3, samplewise: true);
int matrices = perSample.Length; // => 2Remarks — each entry is an ordinary ConfusionMatrix over labels 0 and
1, so Recall.Score, Precision.Score and the rest read
it directly. Under samplewise a row's weight applies to each of that row's labels, because the
matrix counts labels there rather than samples.
Applies to — net10.0, netstandard2.0.
See also — ConfusionMatrix.Compute,
Precision.PerClass, the Python equivalence table.