Metrics precision perclass - 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.

HomeMetricsClassification metrics

Precision.PerClass

Precision for every class, in label order.

public static double[] PerClass(ConfusionMatrix cm, ZeroDivision zeroDivision = ZeroDivision.Zero)
public static double[] PerClass(ReadOnlySpan<int> yTrue, ReadOnlySpan<int> yPred, ZeroDivision zeroDivision = ZeroDivision.Zero, ReadOnlySpan<int> labels = default, ReadOnlySpan<double> sampleWeight = default)

Parameterscm is a matrix already counted, or pass yTrue and yPred. zeroDivision decides an undefined per-class score, labels fixes the label set and its order, and sampleWeight weights the samples.

Returns — a fresh double[], one entry per label, in the matrix's label order.

ExceptionsArgumentNullException when cm is null; ArgumentException when the label spans disagree in length or are empty. 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.

Example — the three-way triage: nothing predicted into class 2 was wrong.

using Lodestar.Metrics;

int[] yTrue = [0, 0, 1, 1, 2, 2, 2];
int[] yPred = [0, 1, 1, 1, 2, 2, 0];

double[] perClass = Precision.PerClass(yTrue, yPred);
double urgent = perClass[0];   // => 0.5
double spam = perClass[2];     // => 1

Remarks — scikit-learn's average=None, as a method because it returns an array. This is the first thing to look at when a macro average is low: it is usually one class, and usually the rarest.

Two traps. The array is positional in the label order, so the score for label 20 is not at index 20; and a class nothing was predicted into contributes a 0.0 here by default, which then drags Averaging.Macro down by a full 1/k even though the model was never asked about it. If that class is absent because your evaluation set is small rather than because the model is bad, ZeroDivision.NaN is the honest setting.

Applies to — net10.0, netstandard2.0.

See alsoPrecision.Score, Recall.PerClass, ClassificationReport.Compute, ZeroDivision, the Python equivalence table.

⚠️ **GitHub.com Fallback** ⚠️