Sub topic 8
Cohen's kappa for two annotators
This chapter assumes you know what observed agreement is and why raw percent agreement can be inflated by chance. Here you compute Cohen's kappa by hand, in plain Python, and with scikit-learn. You also learn what its output means and where it fails.
What kappa measures
Cohen's kappa is an agreement statistic for exactly two annotators who each label the same fixed set of items with categorical labels. It answers one question: how much better do the annotators agree than two annotators who label at random, each keeping their own habitual label frequencies?
The formula is:
kappa = (po - pe) / (1 - p_e)
- p_o is the observed agreement, the share of items where both annotators chose the same label.
- p_e is the agreement expected by chance, computed from each annotator's label frequencies.
- The numerator is the agreement you got beyond chance.
- The denominator is the most agreement beyond chance that was possible.
Kappa is 1 for perfect agreement and 0 when agreement equals chance. It is negative when annotators agree less often than chance predicts.
Computing chance agreement
Chance agreement uses each annotator's marginal distribution, meaning how often each annotator used each label. If annotator A uses "pos" 40% of the time and annotator B uses it 50% of the time, they would both say "pos" on a random item with probability 0.4 × 0.5 = 0.2. You add this product over every label to get p_e.
A worked example by hand
Two annotators label 10 reviews as positive (pos), neutral (neu) or negative (neg).
| Item | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Annotator 1 | pos | pos | neg | neg | pos | neu | neg | pos | neu | neg |
| Annotator 2 | pos | neg | neg | neg | pos | neu | neg | pos | pos | neg |
- They agree on items 1, 3, 4, 5, 6, 7, 8 and 10, so p_o = 8/10 = 0.8.
- Annotator 1 used pos 4 times, neg 4 times and neu 2 times.
- Annotator 2 used pos 4 times, neg 5 times and neu 1 time.
- p_e = (4×4 + 4×5 + 2×1) / 10² = 38/100 = 0.38.
- kappa = (0.8 - 0.38) / (1 - 0.38) = 0.42 / 0.62 ≈ 0.677.
Percent agreement says 80%. Kappa says about 0.68, because with these label frequencies a random pair of annotators would already agree on 38% of items.
Implementing kappa from scratch
from collections import Counter
def cohen_kappa(a, b):
n = len(a)
p_o = sum(x == y for x, y in zip(a, b)) / n
count_a, count_b = Counter(a), Counter(b)
labels = set(a) | set(b)
p_e = sum(count_a[l] * count_b[l] for l in labels) / n ** 2
return (p_o - p_e) / (1 - p_e)
ann1 = ["pos", "pos", "neg", "neg", "pos", "neu", "neg", "pos", "neu", "neg"]
ann2 = ["pos", "neg", "neg", "neg", "pos", "neu", "neg", "pos", "pos", "neg"]
print(round(cohen_kappa(ann1, ann2), 3))The output is 0.677. The union of both annotators' labels matters. If one annotator never used a label, Counter returns 0 for it and that label adds nothing to p_e.
One edge case: if both annotators use a single label for every item, p_e is 1 and the formula divides by zero. Kappa is undefined there, and a real dataset like that usually signals a problem with the task or the sample.
Using scikit-learn and a confusion matrix
In practice, use cohen_kappa_score. Pair it with a confusion matrix, because a single number hides which labels are being confused.
from sklearn.metrics import cohen_kappa_score, confusion_matrix
ann1 = ["pos", "pos", "neg", "neg", "pos", "neu", "neg", "pos", "neu", "neg"]
ann2 = ["pos", "neg", "neg", "neg", "pos", "neu", "neg", "pos", "pos", "neg"]
print(cohen_kappa_score(ann1, ann2))
labels = ["pos", "neu", "neg"]
print(confusion_matrix(ann1, ann2, labels=labels))
# Ordinal labels (for example 1-5 ratings): penalize distant disagreements more
r1 = [1, 2, 3, 4, 5, 3, 2]
r2 = [1, 3, 3, 5, 4, 3, 1]
print(cohen_kappa_score(r1, r2, weights="quadratic"))The first value matches the hand calculation, 0.677. The matrix has annotator 1 on the rows and annotator 2 on the columns:
[[3 0 1]
[1 1 0]
[0 0 4]]The diagonal holds the agreements. The off-diagonal cells show that annotator 1's "pos" became "neg" once, and annotator 1's "neu" became "pos" once. The "neg" row is clean. That tells you where to look in the guidelines.
The third score, about 0.83, uses quadratic weights. With weights, a disagreement between 4 and 5 costs far less than one between 1 and 5. Use this only when the labels have a real order. Unweighted kappa treats every disagreement as equally bad, which is correct for categories such as pos, neu and neg only when you do not consider "neutral" to sit between the other two.
Interpreting the number
Common guidance, from Landis and Koch (1977), is in the table below. Treat it as a rough aid, not a standard.
| Kappa | Common label |
|---|---|
| below 0 | Worse than chance |
| 0.00 to 0.20 | Slight |
| 0.21 to 0.40 | Fair |
| 0.41 to 0.60 | Moderate |
| 0.61 to 0.80 | Substantial |
| 0.81 to 1.00 | Almost perfect |
Our 0.677 falls in "substantial". What counts as good enough depends on the task. Labeling the sentiment of short reviews should reach higher agreement than judging whether a text is sarcastic. Many teams set a target before the pilot round and revise guidelines until they meet it. Also remember that 10 items give a very wide uncertainty range. Report a confidence interval, for example by bootstrapping over items, and do not trust a kappa from a handful of examples.
Where kappa misleads
Prevalence
When one label dominates, pe becomes large and kappa can be low even if annotators agree almost always. Suppose 100 items, where both annotators say "yes" on 90, annotator 1 says "yes" and annotator 2 says "no" on 5, and the reverse on 5. Percent agreement is 0.90. Each annotator says "yes" 95% of the time, so pe = 0.95×0.95 + 0.05×0.05 = 0.905. Kappa is (0.90 - 0.905) / 0.095 ≈ -0.05. The annotators agree 90% of the time, yet kappa says they do no better than chance, because the two disagreements were not on the rare class in any consistent way.
Annotator bias
Two annotators with the same accuracy but different label habits get a different p_e than two annotators with matching habits. Kappa therefore mixes real disagreement with differences in how often each annotator uses each label. Compare the row and column totals of the confusion matrix to spot this.
Cross-study comparison
Kappa values depend on the label set and the label distribution. A 0.7 on a binary task and a 0.7 on a 20-label task are not equivalent, and benchmarks with different class balance are not directly comparable.
Choosing a measure
Cohen's kappa fits a narrow case. Use it when exactly two annotators label every item and the labels are categories.
- For more than two annotators, use Fleiss' kappa or Krippendorff's alpha.
- For ordinal ratings, use weighted kappa.
- For missing labels or mixed scales, use Krippendorff's alpha.
- For entity spans, where the set of items is not fixed, use span-level F1 between annotators.
Exercise
Exercise
Two annotators label 50 texts as "yes" or "no". Both say yes on 20 items. Annotator 1 says yes and annotator 2 says no on 5 items. Annotator 1 says no and annotator 2 says yes on 10 items. Both say no on 15 items. What are the observed agreement, the chance agreement and Cohen's kappa?
Show answer
Observed agreement is (20 + 15) / 50 = 0.70. Annotator 1 says yes on 25 items and no on 25. Annotator 2 says yes on 30 items and no on 20. Chance agreement is (25×30 + 25×20) / 50² = 1250 / 2500 = 0.50. Kappa is (0.70 - 0.50) / (1 - 0.50) = 0.40, which is "fair" to "moderate" on the Landis and Koch scale.
Exercise
Two annotators agree on 96 of 100 items, and both use the label "none" on about 98% of items. Why might kappa still be low, and what else should you report?
Show answer
Chance agreement is already about 0.96 because "none" dominates, so the share of agreement beyond chance is small. Kappa is therefore low or near zero even though percent agreement is 96%. Report the label distribution, percent agreement, and a confusion matrix. Also consider measuring agreement only on items where at least one annotator used a non-"none" label.