Skip to content

Instantly share code, notes, and snippets.

@LouisdeBruijn
Created July 14, 2020 10:35
Show Gist options
  • Select an option

  • Save LouisdeBruijn/9dced57c54e0029e29cdfcfb2e54a8c8 to your computer and use it in GitHub Desktop.

Select an option

Save LouisdeBruijn/9dced57c54e0029e29cdfcfb2e54a8c8 to your computer and use it in GitHub Desktop.
def fleiss_kappa(M):
"""Computes Fleiss' kappa for group of annotators.
:param M: a matrix of shape (:attr:'N', :attr:'k') with 'N' = number of subjects and 'k' = the number of categories.
'M[i, j]' represent the number of raters who assigned the 'i'th subject to the 'j'th category.
:type: numpy matrix
:rtype: float
:return: Fleiss' kappa score
"""
N, k = M.shape # N is # of items, k is # of categories
n_annotators = float(np.sum(M[0, :])) # # of annotators
tot_annotations = N * n_annotators # the total # of annotations
category_sum = np.sum(M, axis=0) # the sum of each category over all items
# chance agreement
p = category_sum / tot_annotations # the distribution of each category over all annotations
PbarE = np.sum(p * p) # average chance agreement over all categories
# observed agreement
P = (np.sum(M * M, axis=1) - n_annotators) / (n_annotators * (n_annotators - 1))
Pbar = np.sum(P) / N # add all observed agreement chances per item and divide by amount of items
return round((Pbar - PbarE) / (1 - PbarE), 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment