induwara.lk
induwara.lkMachine learning · Metrics

Hinge Loss Calculator (SVM / Squared Hinge Loss)

Paste your true labels and decision scores to get the hinge loss max(0, 1 − y·f(x)) — mean or sum, standard or squared — with a per-sample breakdown that shows which points fall inside the margin. Matches scikit-learn's hinge_loss. No signup, runs in your browser.

By Induwara AshinsanaUpdated Jul 12, 2026
Hinge lossSVM / squared hinge
scikit-learn verified

The score is the decision-function value f(x) — a signed distance, not a probability. Separate with a comma, space, or | . Up to 2,000 rows.

Examples
Label convention
Loss variant
Reduction
Hinge · mean
0.75
4 samples
Mean squared hinge
0.995
for comparison
Margin violations
3 / 4
samples with m < 1
Cross-check
✓ matches
via (x+|x|)/2 identity
Reading: 3 of 4 samples fall inside the margin (m < 1) and contribute loss. Loss is dominated by the sample on line 4.

Per-sample breakdown

#yScore f(x)MarginHingeSq. hingeMargin
1+10.80.80.20.04violated
2−1-1.21.200 safe
3+1-0.3-0.31.31.69violated
4−10.5-0.51.52.25violated
Computed in your browser — no data is uploaded.

How it works

Hinge loss is the loss function behind the support vector machine (SVM). For each sample it measures how badly a linear classifier violates the margin — the band of width 2 around the separating hyperplane. This calculator follows the definition published in scikit-learn's sklearn.metrics.hinge_loss reference and the soft-margin formulation introduced by Cortes and Vapnik (1995).

  1. Map the labels. Each true label must be ±1. If your data uses {0, 1}, the tool maps 0 → −1 and 1 → +1 so the margin gets the correct sign (scikit-learn requires y_true ∈ {−1, +1}).
  2. Compute the margin. For every sample, m = y · f(x), where f(x) is the decision-function score (a signed distance), not a probability. A large positive margin means confidently correct; a negative margin means misclassified.
  3. Apply the hinge. Standard hinge: ℓ = max(0, 1 − m). Squared hinge: ℓ = max(0, 1 − m)². Any sample with m ≥ 1 contributes exactly 0 — it is correct and safely outside the margin.
  4. Reduce. The mean (1/N)·Σ ℓᵢ is scikit-learn's default; the sum Σ ℓᵢ is also offered. If you supply per-sample weights, the mean becomes the weighted average Σ wᵢ·ℓᵢ / Σ wᵢ, matching the metric's sample_weight argument.

Every result is verified two ways. The per-sample loss is computed both with Math.max(0, 1 − m) and with the algebraic identity max(0, x) = (x + |x|) / 2; the two agree to floating-point tolerance, which the “Cross-check” tile reports. Because hinge loss consumes decision scores rather than calibrated probabilities, it is the wrong tool for a model that outputs probabilities — reach for log loss (cross-entropy) there instead.

Worked examples

Mixed classifier (the default inputs)

labels {−1,+1}, mean reduction

  1. Samples (y, f): (+1, 0.8) (−1, −1.2) (+1, −0.3) (−1, 0.5)
  2. Margins m = y·f(x): 0.8, 1.2, −0.3, −0.5
  3. Hinge: max(0,1−0.8)=0.2, max(0,1−1.2)=0, max(0,1.3)=1.3, max(0,1.5)=1.5
  4. Mean hinge = (0.2 + 0 + 1.3 + 1.5) / 4 = 3.0 / 4 = 0.75
  5. Squared: (0.04 + 0 + 1.69 + 2.25) / 4 = 3.98 / 4 = 0.995
  6. Margin violated (m < 1) for samples 1, 3, 4 → 3 of 4

Confident, all-correct classifier

every margin ≥ 1

  1. Samples (y, f): (+1, 2.0) (−1, −3.0)
  2. Margins: 2.0 and 3.0 — both ≥ 1
  3. Hinge: max(0, 1 − 2.0) = 0 and max(0, 1 − 3.0) = 0
  4. Mean hinge = 0.0 — loss vanishes even though the scores aren't probabilities

On the margin (off-by-one edge case)

a boundary point contributes nothing

  1. Sample (y, f): (+1, 1.0)
  2. Margin m = 1·1.0 = 1.0
  3. Hinge: max(0, 1 − 1.0) = max(0, 0) = 0
  4. m < 1 is FALSE at exactly 1, so the margin is NOT counted as violated
  5. A point sitting precisely on the margin edge adds zero loss

Frequently asked questions

Sources & references

Definitions and worked examples were last cross-checked against the scikit-learn documentation on 2026-07-12. The binary hinge and squared-hinge formulas here reproduce sklearn.metrics.hinge_loss to the digits shown.

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.