-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCM.py
32 lines (30 loc) · 1.24 KB
/
GCM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#####
# Do multi-classification task,
# then get the ground truth and the predict label named y_true and y_pred
from sklearn import metrics
import numpy as np
y_pred = [1,2,3,4,5,1,2,1,1,4,5]
y_true = [1,2,3,4,5,1,2,1,1,4,1]
def kappa(confusion_matrix, k):
dataMat = np.mat(confusion_matrix)
P0 = 0.0
for i in range(k):
P0 += dataMat[i, i]*1.0
xsum = np.sum(dataMat, axis=1)
ysum = np.sum(dataMat, axis=0)
Pe = float(ysum*xsum)/np.sum(dataMat)**2
OA = float(P0/np.sum(dataMat)*1.0)
cohens_coefficient = float((OA-Pe)/(1-Pe))
return cohens_coefficient
classify_report = metrics.classification_report(y_true, y_pred)
confusion_matrix = metrics.confusion_matrix(y_true, y_pred)
overall_accuracy = metrics.accuracy_score(y_true, y_pred)
acc_for_each_class = metrics.precision_score(y_true, y_pred, average=None)
average_accuracy = np.mean(acc_for_each_class)
kappa_coefficient = kappa(confusion_matrix, 5)
print('classify_report : \n', classify_report)
print('confusion_matrix : \n', confusion_matrix)
print('acc_for_each_class : \n', acc_for_each_class)
print('average_accuracy: {0:f}'.format(average_accuracy))
print('overall_accuracy: {0:f}'.format(overall_accuracy))
print('kappa coefficient: {0:f}'.format(kappa_coefficient))