Skip to content

Instantly share code, notes, and snippets.

@merylldindin
Last active January 11, 2019 03:48
Show Gist options
  • Save merylldindin/4b03e57b74f72daaad55b6d4db4bf98b to your computer and use it in GitHub Desktop.
Save merylldindin/4b03e57b74f72daaad55b6d4db4bf98b to your computer and use it in GitHub Desktop.
from sklearn.metrics import confusion_matrix
def kappa_score(y_true, y_pred):
cfm = confusion_matrix(y_true, y_pred)
n_c = len(np.unique(y_true))
s_0 = np.sum(cfm, axis=0)
s_1 = np.sum(cfm, axis=1)
exp = np.outer(s_0, s_1).astype(np.double) / np.sum(s_0)
mat = np.ones([n_c, n_c], dtype=np.int)
mat.flat[::n_c + 1] = 0
sco = np.sum(mat * cfm) / np.sum(mat * exp)
return 1 - sco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment