Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-070

Cohen's Kappa

Problem

Compute Cohen's kappa: (p_o - p_e) / (1 - p_e), where p_o is the observed agreement and p_e = sum over the union of labels of (true_count_c / n) * (pred_count_c / n).

If p_e == 1.0 or the input is empty, return 0.0.

Hint

Kappa measures agreement above the agreement expected by chance.

Test cases

case 1
in: [[1,1,0,0],[1,1,0,0]]
expected: 1
case 2
in: [[1,1,0,0],[1,0,1,0]]
expected: 0
case 3
in: [[1,1,1,0],[1,1,0,1]]
expected: -0.3333333333333333
case 4
in: [[1,1],[1,1]]
expected: 0
Starter codePython
def cohens_kappa(y_true, y_pred):
    # Your code here
    pass

4 test cases · run your code against them in the editor