Skip to content
Back to skills

Hinge Loss

ASecurity

Compute the hinge_loss metric — provided by scikit-learn. Use when the user has predictions and ground-truth and needs to compute hinge_loss, or asks how to score with hinge_loss.

  • 3 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 11, 2026
documentationpythongoapi

Works with

  • api

Security analysis

A100/100

Scanned September 11, 2026

npx -y skills add qhjqhj00/research-skills-pool --skill hinge-loss --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Hinge Loss?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Hinge Loss
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/qhjqhj00-hinge-loss/badge)](https://www.skillsdirectory.com/skills/qhjqhj00-hinge-loss)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: hinge-loss
description: Compute the hinge_loss metric — provided by scikit-learn. Use when the user has predictions and ground-truth and needs to compute hinge_loss, or asks how to score with hinge_loss.
metadata:
  skill_kind: metric
  source_lib: scikit-learn
  import_path: sklearn.metrics.hinge_loss
  source: library_introspection
---

# hinge-loss

> Metric `hinge_loss` from `scikit-learn` (sklearn.metrics.hinge_loss)

## When to invoke this skill

The user has predictions + ground truth and asks to evaluate with hinge_loss, or
mentions `sklearn.metrics.hinge_loss` directly, or wants the standard scikit-learn implementation.

## Reference signature

```python
from sklearn.metrics import hinge_loss

# hinge_loss(y_true, pred_decision, *, labels=None, sample_weight=None)
```

## Library docstring

```
Average hinge loss (non-regularized).

In binary class case, assuming labels in y_true are encoded with +1 and -1,
when a prediction mistake is made, ``margin = y_true * pred_decision`` is
always negative (since the signs disagree), implying ``1 - margin`` is
always greater than 1.  The cumulated hinge loss is therefore an upper
bound of the number of mistakes made by the classifier.

In multiclass case, the function expects that either all the labels are
included in y_true or an optional labels argument is provided which
contains all the labels. The multilabel margin is calculated according
to Crammer-Singer's method. As in the binary case, the cumulated hinge loss
is an upper bound of the number of mistakes made by the classifier.

Read more in the :ref:`User Guide <hinge_loss>`.

Parameters
----------
y_true : array-like of shape (n_samples,)
    True target, consisting of integers of two values. The positive label
    must be greater than the negative label.

pred_decision : array-like of shape (n_samples,) or (n_samples, n_classes)
    Predicted decisions, as output by decision_function (floats).

labels : array-like, default=None
    Contains all the labels for the problem. Used in multiclass hinge loss.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

Returns
-------
loss : float
    Average hinge loss.

References
----------
.. [1] `Wikipedia entry on the Hinge loss
       <https://en.wikipedia.org/wiki/Hinge_loss>`_.

.. [2] Koby Crammer, Yoram Singer. On the Algorithmic
       Implementation of Multiclass Kernel-based Vector
       Machines. Journal of Machine Learning Research 2,
       (2001), 265-292.

.. [3] `L1 AND L2 Regularization for Multiclass Hinge Loss Models
       by Robert C. Moore, John DeNero
       <https://storage.googleapis.com/pub-tools-public-publication-data/pdf/37362.pdf>`_.

Examples
--------
>>> from sklearn import svm
>>> from sklearn.metrics import hinge_loss
>>> X = [[0], [1]]
>>> y = [-1, 1]
>>> est = svm.LinearSVC(random_state=0)
>>> est.fit(X, y)
LinearSVC(random_state=0)
>>> pred_decision = est.decision_function([[-2], [3], [0.5]])
>>> pred_decision
array([-2.18,  2.36,  0.09])
>>> hin
```

## Quick recipe

```python
import sklearn.metrics as _m
score = _m.hinge_loss(y_true, y_pred)
```

## Don'ts

- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is `(y_true, y_pred)` while torchmetrics is `(preds, target)`.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.