|
a |
|
b/python/aux1/testing_kappa.py |
|
|
1 |
|
|
|
2 |
from sklearn import metrics |
|
|
3 |
import numpy as np |
|
|
4 |
|
|
|
5 |
def compute_cohen_kappa(confusion_matrix): |
|
|
6 |
prob_expectedA = np.empty(len(confusion_matrix)) |
|
|
7 |
prob_expectedB = np.empty(len(confusion_matrix)) |
|
|
8 |
prob_observed = 0 |
|
|
9 |
|
|
|
10 |
mean_acc = np.empty(len(confusion_matrix)) |
|
|
11 |
for n in range(0, len(confusion_matrix)): |
|
|
12 |
prob_expectedA[n] = sum(confusion_matrix[n,:]) / sum(sum(confusion_matrix)) |
|
|
13 |
prob_expectedB[n] = sum(confusion_matrix[:,n]) / sum(sum(confusion_matrix)) |
|
|
14 |
|
|
|
15 |
prob_observed = prob_observed + confusion_matrix[n][n] |
|
|
16 |
mean_acc[n] = confusion_matrix[n][n] / sum(confusion_matrix[:,n]) |
|
|
17 |
|
|
|
18 |
prob_expected = np.dot(prob_expectedA, prob_expectedB) |
|
|
19 |
prob_observed = prob_observed / sum(sum(confusion_matrix)) |
|
|
20 |
|
|
|
21 |
kappa = (prob_observed - prob_expected) / (1 - prob_expected) |
|
|
22 |
|
|
|
23 |
print("Kappa: " + str(kappa) + " Po: " + str(prob_observed) + " Pe: " + str(prob_expected) + "\nPe_a: " + str(prob_expectedA) + "\nPe_b: " + str(prob_expectedB) ) |
|
|
24 |
print("Mean acc = " + str(np.sum(mean_acc) / 4.) + " m_acc = " + str(mean_acc)) |
|
|
25 |
|
|
|
26 |
print("Kappa / Acc") |
|
|
27 |
print(str(kappa / prob_observed)) |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
print("Linear relationship y = mx + w:\t prob_observed * (1./0.75) + (0.25/0.75)") |
|
|
31 |
print( str( (prob_observed * (1./0.75) + (-0.25/0.75) ))) |
|
|
32 |
|
|
|
33 |
return kappa, prob_observed, prob_expected |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
conf_mat = np.array([[33881, 2531, 2385, 5236], [263, 1036, 725, 26], [63, 350, 2584, 223], [43, 2, 4, 339]]) |
|
|
37 |
print(conf_mat) |
|
|
38 |
conf_mat = conf_mat.astype(float) |
|
|
39 |
compute_cohen_kappa(conf_mat) |
|
|
40 |
|
|
|
41 |
print("\n\n") |
|
|
42 |
conf_mat = np.array([[25171, 11907, 1125, 5830], [340, 1474, 39, 197], [212, 486, 2369, 153], [31, 5, 55, 297]]) |
|
|
43 |
print(conf_mat) |
|
|
44 |
conf_mat = conf_mat.astype(float) |
|
|
45 |
compute_cohen_kappa(conf_mat) |
|
|
46 |
|
|
|
47 |
conf_mat = np.array([[50, 0,0 ,0 ], [0, 50, 0, 0], [0, 0, 50, 0], [0, 0, 0, 50]]) |
|
|
48 |
print(conf_mat) |
|
|
49 |
conf_mat = conf_mat.astype(float) |
|
|
50 |
compute_cohen_kappa(conf_mat) |
|
|
51 |
|
|
|
52 |
print("\n\n") |
|
|
53 |
conf_mat = np.array([[40, 0, 5 ,5 ], [3, 35, 2, 10], [2, 3, 45, 0], [0, 0, 0, 50]]) |
|
|
54 |
print(conf_mat) |
|
|
55 |
conf_mat = conf_mat.astype(float) |
|
|
56 |
compute_cohen_kappa(conf_mat) |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
print("\n\n") |
|
|
60 |
conf_mat = np.array([[20, 0, 15 ,15 ], [5, 33, 5, 7], [3, 3, 43,1], [1, 6, 3, 40]]) |
|
|
61 |
print(conf_mat) |
|
|
62 |
conf_mat = conf_mat.astype(float) |
|
|
63 |
compute_cohen_kappa(conf_mat) |
|
|
64 |
|
|
|
65 |
|
|
|
66 |
print("\n\n") |
|
|
67 |
conf_mat = np.array([[10, 10, 15 ,15 ], [5, 33, 5, 7], [6, 0, 3,41], [1, 6, 3, 40]]) |
|
|
68 |
print(conf_mat) |
|
|
69 |
conf_mat = conf_mat.astype(float) |
|
|
70 |
compute_cohen_kappa(conf_mat) |