#!/usr/bin/env python3
# Source: t6-practitioner-toolkit.html
import numpy as np

def cohens_kappa(a, b):
    """a, b: label arrays from two raters on the same items."""
    a, b = np.asarray(a), np.asarray(b)
    labels = np.union1d(a, b)
    p_o = np.mean(a == b)
    # chance agreement: sum over classes of P(rater A says k) * P(rater B says k)
    p_e = sum(np.mean(a == k) * np.mean(b == k) for k in labels)
    return (p_o - p_e) / (1 - p_e)

a = [1, 1, 0, 1, 0, 0, 1, 1, 0, 1]
b = [1, 0, 0, 1, 0, 1, 1, 1, 0, 1]
print(round(cohens_kappa(a, b), 3))   # 0.583
