-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics.py
More file actions
55 lines (37 loc) · 1.53 KB
/
Copy pathmetrics.py
File metadata and controls
55 lines (37 loc) · 1.53 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
from typing import List
from numpy import ndarray
from sklearn.metrics import accuracy_score
def get_npcer(false_negative: int, true_positive: int):
return false_negative / (false_negative + true_positive)
def get_apcer(false_positive: int, true_negative: int):
return false_positive / (true_negative + false_positive)
def get_acer(apcer: float, npcer: float):
return (apcer + npcer) / 2.0
def get_metrics(pred: ndarray, targets: ndarray):
negative_indices = targets == 0
positive_indices = targets == 1
false_positive = (pred[negative_indices] == 1).sum()
false_negative = (pred[positive_indices] == 0).sum()
true_positive = (pred[positive_indices] == 1).sum()
true_negative = (pred[negative_indices] == 0).sum()
npcer = get_npcer(false_negative, true_positive)
apcer = get_apcer(false_positive, true_negative)
acer = get_acer(apcer, npcer)
return acer, apcer, npcer
def get_threshold(probs: ndarray, grid_density: int = 10):
min_, max_ = min(probs), max(probs)
thresholds = [min_]
for i in range(grid_density + 1):
thresholds.append(min_ + (i * (max_ - min_)) / float(grid_density))
thresholds.append(1.1)
return thresholds
def eval_from_scores(scores: ndarray, targets: ndarray):
thrs = get_threshold(scores)
acc = 0.0
best_thr = -1
for thr in thrs:
acc_new = accuracy_score(targets, scores >= thr)
if acc_new > acc:
best_thr = thr
acc = acc_new
return get_metrics(scores >= best_thr, targets), best_thr, acc