Metrics hingeloss 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.

HomeMetricsClassification metrics

HingeLoss.Score

The binary hinge loss — sklearn.metrics.hinge_loss.

public static double Score(ReadOnlySpan<int> yTrue, ReadOnlySpan<double> predDecision, int posLabel = 1, ReadOnlySpan<double> sampleWeight = default)

ParametersyTrue is the true labels, one per sample. predDecision is the decision value per sample: positive on posLabel's side of the boundary, and further from zero the more confident — not a probability. posLabel is the label on the positive side, 1 by default. sampleWeight is one weight per sample, or empty.

Returnsdouble, 0 or above and unbounded. 0 when every sample sits on the right side by a margin of at least 1.

ExceptionsArgumentException when the inputs disagree in length, are empty, or the weights do not match. yTrue holding more than two distinct labels is refused with the reference's "The shape of pred_decision cannot be 1d array with a multiclass target." rather than counting the third as negative — HingeLoss.MultiClass scores it. A decision holding NaN or an infinity is refused with the reference's "Input contains NaN." or its infinity counterpart, naming predDecision. Weights summing to zero are refused with numpy's "Weights sum to zero, can't be normalized."; hinge_loss never calls _check_sample_weight, so a non-finite weight is scored, NaN on both sides.

Example — four samples, two of them inside the margin.

using Lodestar.Metrics;

int[] truth = [-1, 1, 1, -1];
double[] decisions = [-0.5, 1.2, 0.3, 0.8];

double loss = HingeLoss.Score(truth, decisions);  // => 0.75

The second sample is right by more than 1 and costs nothing; the fourth is on the wrong side by 0.8 and costs 1.8.

Remarks — only the sign of the decision is compared against the label, so relabelling cannot move the number. On a truth carrying one class only, scikit-learn returns a value computed against the wrong side — 1.65 where the margins say 0.35 — because its LabelBinarizer has nothing to contrast; the type page has that divergence in full.

Applies to — net10.0, netstandard2.0.

See alsoHingeLoss.MultiClass, ZeroOneLoss.Score, LogLoss.Score, the Python equivalence table.

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