Metrics logloss multiclass - 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
The cross-entropy over a probability matrix — sklearn.metrics.log_loss with 2-D probabilities.
public static double MultiClass(ReadOnlySpan<int> yTrue, ReadOnlySpan<double> yProba, int classCount, bool normalize = true, ReadOnlySpan<double> sampleWeight = default)Parameters — yTrue is the true class index of each sample, in [0, classCount). yProba is
the class probabilities row-major: sample 0's classes, then sample 1's. classCount is how many
classes each row scores. normalize divides by the total weight; pass false for the sum.
sampleWeight is one weight per sample, or empty.
Returns — double, 0 or above and unbounded. Only the column of the true class contributes,
so the score depends on the other columns solely through whatever normalisation the caller applied.
Exceptions — ArgumentException when yProba is not yTrue.Length × classCount, when a label
is not a class index below classCount, or when a probability falls outside [0, 1]. 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 refused too, with numpy's "Weights sum to zero, can't be normalized." — only while normalize is true.
ArgumentOutOfRangeException when classCount is below two.
Example — four samples over three classes.
using Lodestar.Metrics;
int[] truth = [0, 1, 2, 1];
double[] probabilities =
[
0.7, 0.2, 0.1,
0.1, 0.8, 0.1,
0.2, 0.2, 0.6,
0.3, 0.4, 0.3,
];
double loss = LogLoss.MultiClass(truth, probabilities, classCount: 3); // => 0.5017…Remarks — a row that does not sum to 1 is neither refused nor renormalised. The reference warns
and scores the values as given; there is no warning channel here, so the number is the only signal —
measured, halving every row above takes the loss to 1.1948…. This is the one place where
RocAuc.MultiClass is stricter than its own reference and this is not:
that one refuses a row that does not sum to 1.
Applies to — net10.0, netstandard2.0.
See also — LogLoss.Score, BrierScore.MultiClass,
the Python equivalence table.