Hamming Loss

Machine Learning
Introduced by
Information theory and multi-label learning
Year
2000s
Type
Loss function
Family
Multi-label Metric

What does it do?

Hamming loss is the fraction of label positions that a multi-label classifier gets wrong, computed over every sample and every label.

Each sample's predicted label vector is compared position by position with its true label vector, so both missed labels and wrongly added labels count as errors. The value ranges from 0 (every position correct) to 1 (every position wrong).

The intuition behind the method

In multi-label classification, each sample can carry several labels at once, so a prediction can be partly right. Hamming loss treats every label position as one yes/no decision and counts how many of these decisions disagree with the truth. A label that is wrongly assigned and a label that is wrongly omitted each count as one mismatch. The total is divided by the number of label positions in the whole dataset, giving an error rate. Because a sample with one wrong label out of many is penalised only for that label, the metric gives partial credit where 0/1 loss would count the whole sample as wrong.

History

Hamming loss takes its name from the Hamming distance of coding theory, which counts the positions at which two equal-length sequences differ. Hamming loss is this count divided by the number of label positions, so it is a normalised error rate rather than a raw distance. The adoption of the measure for multi-label evaluation has no single documented originator.

When to use it

Use Hamming loss when each sample can have several labels, when partially correct label sets are meaningful, and when every label is roughly equally important to the task. It suits document tagging, image annotation and similar settings where one missed tag is a smaller failure than a wholly wrong tag set. Do not rely on it alone when exact label-set correctness is required, since subset accuracy is the stricter criterion. Also avoid it as the main measure when a few rare labels matter most, because the overall value is driven by the bulk of label positions and can hide failures on those labels. In such cases, examine per-label precision and recall.

Limitations and considerations

  • False positives and false negatives are counted identically, so the value cannot show whether a model tends to over-assign or under-assign labels.
  • The denominator includes every negative label position, so on sparse label matrices a predictor that rarely assigns labels can achieve a low loss.
  • Each position is scored independently and label correlations are ignored: two predictions with the same number of wrong positions receive the same loss even if one error pattern is far less plausible than the other.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Each mismatched label position adds one unit to the count, so a sample with a single wrong label out of many receives a small penalty rather than a full error.
  • Because the result is a fraction of all label positions, it reads directly as an overall error rate across the sample-by-label matrix.
  • Each position is scored with a simple equality comparison, so the calculation is a single pass over the true and predicted label matrices.

Applications

  • Document classification with multiple topics, where each document can have several relevant categories.
  • Image annotation, where each image can have multiple relevant tags or objects.
  • Disease diagnosis, where a patient can have multiple concurrent conditions.
  • Gene annotation, where each gene can have multiple associated functions or pathways.

How it works

How it works

1

Each sample has a true label vector with one entry per possible label, marking which labels apply, and a predicted label vector of the same length. Both vectors are binary, so any continuous model scores must first be converted into labels with a decision threshold, and that threshold should be reported alongside the value.

2

For each sample, count the positions where the predicted entry differs from the true entry, whether the label was wrongly added or wrongly omitted. Sum these mismatch counts over all samples and divide by the number of samples multiplied by the number of labels, which is the total number of label positions.

3

The result lies between 0 and 1, and lower is better. A value of 0 means every label position matches; a value of 1 means every position is wrong. Because the denominator includes all negative positions as well as positive ones, the value should be read against a simple baseline, such as predicting the most common label pattern for every instance.

Common pitfalls

  • Reporting only the overall value can hide poor performance on individual labels; per-label precision and recall reveal these failures.
  • Raw values are not directly comparable across datasets with different label distributions, because Hamming loss depends on label density and sparsity: on sparse label matrices a predictor that rarely assigns labels can achieve a low loss. Compare each model against a baseline on the same dataset.
  • Confusing Hamming loss with subset accuracy (0/1 loss): one wrong label adds only one mismatch to Hamming loss, but makes the whole sample wrong under subset accuracy.

Frequently asked questions

How does Hamming loss differ from 0/1 loss (subset accuracy)?

Hamming loss counts individual incorrect label positions and averages them over all positions, so it gives partial credit. Subset accuracy (0/1 loss) counts a sample as wrong if any label is wrong, so it requires the entire label set to be correct. Subset accuracy is therefore the stricter of the two.

What counts as a good Hamming loss value?

There is no universal threshold, because the acceptable value depends on the dataset, the number of labels and how sparse the labels are. Compare the value with simple baselines, such as predicting the most common label pattern for every instance, and check per-label errors before judging the model.

Should I use Hamming loss if labels are imbalanced?

Hamming loss treats all label positions equally, so the many negative positions can dominate the result, and frequent labels can drive the overall value. For imbalanced multi-label data, examine per-label precision and recall separately to confirm that minority labels are not being ignored.

Related methods

Sources

  1. Schapire, R. E., & Singer, Y. (2000). BoosTexter: A boosting-based system for text categorization. Machine Learning, 39(2-3), 135-168.

    Open DOI reference
  2. Tsoumakas, G., & Katakis, I. (2007). Multi-label classification: An overview. International Journal of Data Warehousing and Mining, 3(3), 1-13.

    Open DOI reference