Compute the hamming_loss metric — provided by scikit-learn. Use when the user has predictions and ground-truth and needs to compute hamming_loss, or asks how to score with hamming_loss.
Installs into .claude/skills of the current project.
Are you the author of Hamming Loss?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/qhjqhj00-hamming-loss)
---
name: hamming-loss
description: Compute the hamming_loss metric — provided by scikit-learn. Use when the user has predictions and ground-truth and needs to compute hamming_loss, or asks how to score with hamming_loss.
metadata:
skill_kind: metric
source_lib: scikit-learn
import_path: sklearn.metrics.hamming_loss
source: library_introspection
---
# hamming-loss
> Metric `hamming_loss` from `scikit-learn` (sklearn.metrics.hamming_loss)
## When to invoke this skill
The user has predictions + ground truth and asks to evaluate with hamming_loss, or
mentions `sklearn.metrics.hamming_loss` directly, or wants the standard scikit-learn implementation.
## Reference signature
```python
from sklearn.metrics import hamming_loss
# hamming_loss(y_true, y_pred, *, sample_weight=None)
```
## Library docstring
```
Compute the average Hamming loss.
The Hamming loss is the fraction of labels that are incorrectly predicted.
Read more in the :ref:`User Guide <hamming_loss>`.
Parameters
----------
y_true : 1d array-like, or label indicator array / sparse matrix
Ground truth (correct) labels. Sparse matrix is only supported when
targets are of :term:`multilabel` type.
y_pred : 1d array-like, or label indicator array / sparse matrix
Predicted labels, as returned by a classifier. Sparse matrix is only
supported when targets are of :term:`multilabel` type.
sample_weight : array-like of shape (n_samples,), default=None
Sample weights.
.. versionadded:: 0.18
Returns
-------
loss : float
Returns the average Hamming loss between element of ``y_true`` and
``y_pred``.
See Also
--------
accuracy_score : Compute the accuracy score. By default, the function will
return the fraction of correct predictions divided by the total number
of predictions.
jaccard_score : Compute the Jaccard similarity coefficient score.
zero_one_loss : Compute the Zero-one classification loss. By default, the
function will return the percentage of imperfectly predicted subsets.
Notes
-----
In multiclass classification, the Hamming loss corresponds to the Hamming
distance between ``y_true`` and ``y_pred`` which is equivalent to the
subset ``zero_one_loss`` function, when `normalize` parameter is set to
True.
In multilabel classification, the Hamming loss is different from the
subset zero-one loss. The zero-one loss considers the entire set of labels
for a given sample incorrect if it does not entirely match the true set of
labels. Hamming loss is more forgiving in that it penalizes only the
individual labels.
The Hamming loss is upperbounded by the subset zero-one loss, when
`normalize` parameter is set to True. It is always between 0 and 1,
lower being better.
References
----------
.. [1] Grigorios Tsoumakas, Ioannis Katakis. Multi-Label Classification:
An Overview. International Journal of Data Warehousing & Mining,
3(3), 1-13, July-September 2007.
.. [2] `Wikipedia entry on the Hamming distance
<https://en.wikipedia.org/wiki/Hamming_di
```
## Quick recipe
```python
import sklearn.metrics as _m
score = _m.hamming_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)`.