Metrics hammingloss score - 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 fraction of labels predicted wrongly — sklearn.metrics.hamming_loss.
public static double Score(ReadOnlySpan<int> yTrue, ReadOnlySpan<int> yPred, ReadOnlySpan<double> sampleWeight = default)
public static double Score(ReadOnlySpan<bool> yTrue, ReadOnlySpan<bool> yPred, int labelCount, ReadOnlySpan<double> sampleWeight = default)Parameters — the first overload takes yTrue and yPred as one label per sample. The second
takes them as a label matrix: one boolean per label per sample, row-major, labelCount values per
row. sampleWeight is one weight per sample — per row, not per label — or empty, the default.
Returns — double in [0, 1]. 0 when everything is right.
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. On labels, weights that merely sum to zero are refused too, with numpy's "Weights sum to zero, can't be normalized."; on a label matrix they are not, and the answer is -∞ or NaN, as scikit-learn's is.
Example — four samples over three classes, one wrong.
using Lodestar.Metrics;
int[] truth = [0, 1, 2, 1];
int[] predicted = [0, 2, 2, 1];
double loss = HammingLoss.Score(truth, predicted); // => 0.25Over a matrix it counts labels rather than samples, which is where it and
ZeroOneLoss.Score differ:
using Lodestar.Metrics;
bool[] truth = [true, false, true, false, true, true];
bool[] predicted = [true, false, false, true, true, true];
double loss = HammingLoss.Score(truth, predicted, labelCount: 3); // => 0.3333…Two of the six labels are wrong. ZeroOneLoss.Score reads 1 on the same input, because both rows
carry a mistake.
Applies to — net10.0, netstandard2.0.
See also — ZeroOneLoss.Score, Accuracy.Score,
the Python equivalence table.