-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfusionMatrix.py
58 lines (47 loc) · 1.51 KB
/
ConfusionMatrix.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Confusion:
def __init__(self):
self.tp = 0
self.fp = 0
self.fn = 0
self.tn = 0
def precision(self):
if self.tp + self.fp == 0:
return 0
return self.tp / (self.tp + self.fp)
def recall(self):
if self.tp + self.fn == 0:
return 0
return self.tp / (self.tp + self.fn)
def accuracy(self):
if self.tp + self.tn + self.fp + self.fn == 0:
return 0
return (self.tp + self.tn) / (self.tp + self.tn + self.fp + self.fn)
def fb(self, b):
p = self.precision()
r = self.recall()
if p == 0 and r == 0:
return 0
return (1 + b ** 2) * (p * r) / (b ** 2 * p + r)
class ConfusionMatrix:
def __init__(self, number_of_classes):
self.matrix = [Confusion() for _ in range(number_of_classes)]
def add(self, predicted, actual):
for i in range(len(self.matrix)):
if i == predicted:
if i == actual:
self.matrix[i].tp += 1
else:
self.matrix[i].fp += 1
else:
if i == actual:
self.matrix[i].fn += 1
else:
self.matrix[i].tn += 1
def precision(self, i):
return self.matrix[i].precision()
def recall(self, i):
return self.matrix[i].recall()
def accuracy(self, i):
return self.matrix[i].accuracy()
def fb(self, b, i):
return self.matrix[i].fb(b)