Repository navigation
Expand file tree
/
Copy pathStatistics.py
More file actions
138 lines (90 loc) · 2.48 KB
/
Copy pathStatistics.py
File metadata and controls
138 lines (90 loc) · 2.48 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import numpy as np
def confusion(labels, predictions, dim=0, log=True):
if dim <= 0:
for label in labels:
if int(label) >= dim:
dim = int(label)+1
for label in predictions:
if int(label) >= dim:
dim = int(label) + 1
cm = [[0] * dim for _ in range(dim)]
for i in range(min(len(labels), len(predictions))):
cm[int(labels[i])][int(predictions[i])] += 1
if log:
print("Confusion Matrix:")
for mst in cm:
print(str(mst))
return cm
def precision(cm, log=True, verbose=True):
dim = len(cm)
prs, pr = [], 0
for i in range(dim):
summary = 0
for j in range(dim):
summary += cm[j][i]
if summary == 0:
tpr = 1.0
else:
tpr = cm[i][i] / summary
pr += tpr
prs.append(tpr)
if log and verbose:
print("Precision on class %s: %s" % (i, tpr))
pr /= dim
if log:
print("Precision mean: %s" % pr)
return pr, prs
def recall(cm, log=True, verbose=True):
dim = len(cm)
rcs, rc = [], 0
for i in range(dim):
summary = 0
for j in range(dim):
summary += cm[i][j]
if summary == 0:
trc = 1.0
else:
trc = cm[i][i] / summary
rc += trc
rcs.append(trc)
if log and verbose:
print("Recall on class %d: %f" % (i, trc))
rc /= dim
if log:
print("Recall mean: %s" % rc)
return rc, rcs
def accuracy(cm, log=True):
dim = len(cm)
acc, summary = 0, 0
for i in range(dim):
for j in range(dim):
summary += cm[i][j]
acc += cm[i][i]
acc /= summary
if log:
print("Accuracy: %s" % acc)
return acc
def fullstats(labels, predictions, dim=0, printing=True, verbose=True):
cm = confusion(labels, predictions, dim, printing)
pr, prs = precision(cm, printing, verbose)
rc, rcs = recall(cm, printing, verbose)
return cm, pr, rc, prs, rcs
def unittest():
cm = np.ones(shape=(1000, 1000), dtype=np.float32)
assert np.isclose(precision(cm, verbose=False)[0], 0.001)
assert np.isclose(recall(cm, verbose=False)[0], 0.001)
assert np.isclose(accuracy(cm), 0.001)
import random
labels = [random.randint(0, 5) for _ in range(10000)]
results = [random.randint(0, 5) for _ in range(10000)]
cm, pr, rc, prs, rcs = fullstats(labels, results, verbose=False)
labels = np.array(labels)
results = np.array(results)
npcm, nppr, nprc, npprs, nprcs = fullstats(labels, results, verbose=False)
assert np.allclose(np.array(cm), npcm)
assert np.allclose(np.array(pr), nppr)
assert np.allclose(np.array(rc), nprc)
assert np.allclose(np.array(prs), npprs)
assert np.allclose(np.array(rcs), nprcs)
if __name__ == "__main__":
unittest()