Sub topic 8
Cohen's Kappa for Two Annotators
Prerequisites and purpose
This chapter assumes you know basic Python, proportions, and common NLP tasks such as text classification. You should also understand that an annotator is a person who assigns labels to text.
Cohen's kappa measures agreement between two annotators who label the same fixed set of items. It improves on percent agreement by asking how much agreement would be expected if the annotators used their labels according to their overall label frequencies.
Use it for categorical labels such as pos, neg, and neu. Each item must receive one label from each annotator.
From observed agreement to kappa
Observed agreement is the proportion of items where the two annotators chose the same label:
p_o = \frac{\text{matching labels}}{\text{all items}}Chance agreement, written as \(p_e\), uses each annotator's label frequencies:
p_e = \sum_l P_A(l)P_B(l)Here, \(l\) is a label. Cohen's kappa is:
\kappa = \frac{p_o - p_e}{1-p_e}The numerator removes agreement that could be explained by label frequencies. The denominator scales the result so that 1 means complete agreement and 0 means agreement no better than this chance baseline.
from collections import Counter
def cohen_kappa(a, b):
if len(a) != len(b) or not a:
raise ValueError("Inputs must have the same nonzero length")
n = len(a)
observed = sum(x == y for x, y in zip(a, b)) / n
count_a = Counter(a)
count_b = Counter(b)
labels = set(a) | set(b)
expected = sum(count_a[label] * count_b[label]
for label in labels) / n**2
if expected == 1:
return 1.0
return (observed - expected) / (1 - expected)
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 annotators agree on 8 of 10 items, so \(po=0.8\). Their label counts produce \(pe=0.38\). Therefore:
\kappa = \frac{0.8-0.38}{1-0.38}\approx0.677Interpreting and checking the result
Kappa is often described with rough bands such as poor, moderate, or substantial agreement. Treat these bands as communication aids, not universal scientific thresholds. The meaning of 0.677 depends on the task, label definitions, class balance, and cost of disagreement.
Always inspect the raw agreement and a confusion matrix. Kappa can be low when a rare label is important, and it can change when annotators have different labeling tendencies. Analyze which labels are confused before revising guidelines or accepting the dataset.
Exercise
Two annotators agree on 90 of 100 items. If their expected chance agreement is 0.80, what is Cohen's kappa?
Show answer
Kappa is (0.90 - 0.80) / (1 - 0.80) = 0.50.