forked from nkarasiak/HistoricalMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccuracy_index.py
37 lines (28 loc) · 980 Bytes
/
accuracy_index.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
33
34
35
36
37
#! /usr/bin/python3
"""!@brief Accuracy index by Mathieu Fauvel
"""
from __future__ import annotations
import numpy as np
class CONFUSION_MATRIX:
def __init__(self):
self.confusion_matrix = None
self.OA = None
self.Kappa = None
def compute_confusion_matrix(self, yp, yr):
"""
Compute the confusion matrix
"""
# Initialization
n = yp.size
C = int(yr.max())
self.confusion_matrix = np.zeros((C, C))
# Compute confusion matrix
for i in range(n):
self.confusion_matrix[yp[i].astype(int) - 1, yr[i].astype(int) - 1] += 1
# Compute overall accuracy
self.OA = np.sum(np.diag(self.confusion_matrix)) / n
# Compute Kappa
nl = np.sum(self.confusion_matrix, axis=1)
nc = np.sum(self.confusion_matrix, axis=0)
self.Kappa = ((n**2) * self.OA - np.sum(nc * nl)) / (n**2 - np.sum(nc * nl))
# TBD Variance du Kappa