Skip to content

Metrics multilabelconfusionmatrix compute

github-actions[bot] edited this page Aug 26, 2026 · 18 revisions

Development build. This page describes main, not a released package. The latest published Lodestar.Metrics is 0.3.0 — read its documentation.

MultilabelConfusionMatrix.Compute

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.

ExceptionsArgumentException 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.

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;  // => 3

Counting 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;  // => 2

Remarks — 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 alsoConfusionMatrix.Compute, Precision.PerClass, the Python equivalence table.

Lodestar

Project

Clone this wiki locally