Inter-annotator agreement in Python: a worked Cohen's kappa example

For companies4 min read

Inter-annotator agreement tells you how often independent annotators give the same answer once chance is removed. For two annotators and a categorical label, Cohen's kappa is the standard first check. This post does one example by hand, then in Python.

The idea in one formula

Raw agreement is the share of items where two annotators gave the same label. Kappa corrects it for the agreement you would expect by chance. The formula is kappa = (p_o - p_e) / (1 - p_e), where p_o is observed agreement and p_e is the agreement expected if each annotator labelled at random according to their own label frequencies. A value of 1 is full agreement and 0 is no better than chance.

Source: scikit-learn: cohen_kappa_score(opens in a new tab)

A worked example with ten items

Two experts mark ten model answers as pass or fail. They agree on 8 of 10 items. Each marked 7 as pass and 3 as fail.

Item12345678910
Expert Apasspassfailpassfailpasspassfailpasspass
Expert Bpasspassfailfailfailpasspasspasspasspass
  1. Observed agreement p_o = 8 / 10 = 0.80.
  2. Chance agreement p_e = (0.7 x 0.7) + (0.3 x 0.3) = 0.49 + 0.09 = 0.58.
  3. Kappa = (0.80 - 0.58) / (1 - 0.58) = 0.22 / 0.42, about 0.52.

Eighty percent sounds good. A kappa of 0.52 is far more modest, because with seven passes in ten, two people guessing would already agree 58 percent of the time.

The same thing in Python

Python
from sklearn.metrics import cohen_kappa_score a = ["pass","pass","fail","pass","fail","pass","pass","fail","pass","pass"]b = ["pass","pass","fail","fail","fail","pass","pass","pass","pass","pass"] raw = sum(x == y for x, y in zip(a, b)) / len(a)kappa = cohen_kappa_score(a, b)print(f"raw agreement {raw:.2f}, kappa {kappa:.2f}")

The function takes two label lists of the same length and returns a value between -1 and 1. Swapping the lists does not change the result. For ordinal scales such as 1 to 5 ratings, it also accepts a weights option, with linear or quadratic choices, so that a one-point miss counts as less serious than a four-point miss.

Reading the number

Many teams quote the Landis and Koch bands: 0.41 to 0.60 moderate, 0.61 to 0.80 substantial, above 0.80 almost perfect. Treat them as habit, not law. The Wikipedia article on kappa notes that the authors gave no evidence for the bands and that they are not universally accepted.

Source: Wikipedia: Cohen's kappa(opens in a new tab)

The better question is what your use needs. If a wrong label is cheap, 0.6 may do. If the label drives a safety decision, ask for more and read the disagreements one by one.

Three traps

  • Skewed labels. When one label dominates, kappa can look low even if raw agreement is high. Report both, and look at the table of counts.
  • Different annotator pairs. Compute kappa for each pair rather than only the pooled value. One weak annotator can pull the average down and hide who needs help.
  • Undefined results. The scikit-learn function returns nan when a label is missing from one annotator's data, and raises a warning. Check for it.

When you need more than kappa

Cohen's kappa is for two raters. With three or more raters, missing ratings or ordinal and numeric scores, use Fleiss' kappa or Krippendorff's alpha. See the separate post on choosing between them.

What to do with the number

  1. Compute agreement on a double-annotated slice every batch.
  2. Sort the disagreements by label pair and read ten of them.
  3. Decide whether each is a guideline gap, a skill gap or a genuinely ambiguous item.
  4. Fix the cause and measure again on the next batch.

A small check you can run today

You do not need a large dataset to start. Take 30 items your team has already labelled, give them to a second expert without showing the first expert's answers, and compute kappa. Even this small run will show whether a label is being used the same way by two people. With only 30 items, the number will be noisy, so use it to find disagreements to read, not to make a final claim about quality.

Then sort the disagreements by pair of labels. If most of them are between two specific labels, the guideline probably does not separate those labels well. If they are scattered, the problem is more likely the task or the annotators' skill.

Agreement is a diagnostic, not a goal. Two annotators who share the same mistake agree perfectly, which is why gold items and expert review stay in the loop.