-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
69 lines (51 loc) · 2.07 KB
/
evaluate.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
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import pandas as pd
import random
from sklearn.metrics import precision_recall_curve
def get_class_threshold(y_true, y_pred_prob, target_class_precision=0.95):
if len(y_true) == 0:
return 1.1
if sum(y_true) == 0:
return 1.1
precision, _, thresholds = precision_recall_curve(y_true, y_pred_prob)
idxs = np.where(precision > target_class_precision)[0]
if len(idxs) == 0 or idxs[0] >= len(thresholds):
return 1.1
return thresholds[idxs[0]]
def get_multiclass_tresholds(y_true, y_pred_probs, labels, target_class_precision=0.95):
class_thresholds = dict()
labels_predicted = np.argmax(np.asarray(y_pred_probs), axis=1)
max_probs = np.max(np.asarray(y_pred_probs), axis=1)
for i, label in enumerate(labels):
label_predicted_idxs = np.where(labels_predicted == i)[0]
y_pred_probs_label = np.take(max_probs, label_predicted_idxs)
y_true_label = np.take(y_true, label_predicted_idxs) == label
y_true_label = y_true_label.astype(int)
class_thresholds[label] = get_class_threshold(
y_true_label,
y_pred_probs_label,
target_class_precision=target_class_precision
)
return class_thresholds
def predict_multiclass_by_thresholds(y_pred_probs, labels, thresholds, unknown_label="UNK"):
winning_labels = [labels[i] for i in np.argmax(y_pred_probs, axis=1)]
winning_probs = np.max(y_pred_probs, axis=1)
result = []
for label, prob in zip(winning_labels, winning_probs):
if prob >= thresholds[label]:
result.append(label)
else:
result.append(unknown_label)
return result
def predict_labels(test_true_labels, test_pred_probs, eval_pred_probs, class_names, target_class_precision=0.95):
thresholds = get_multiclass_tresholds(
test_true_labels,
test_pred_probs,
class_names,
target_class_precision
)
return predict_multiclass_by_thresholds(
eval_pred_probs,
class_names,
thresholds
)