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.
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).
- 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}).
- 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. - 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. - 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'ssample_weightargument.
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
Frequently asked questions
Sources & references
- scikit-learn — sklearn.metrics.hinge_loss (API reference)
- scikit-learn User Guide §3.3 — Hinge loss definition and averaging
- Cortes & Vapnik (1995) — Support-Vector Networks, Machine Learning 20(3)
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
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.