-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
66 lines (41 loc) · 1.83 KB
/
metrics.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
import numpy as np
def accuracy_score(target: np.ndarray, prediction: np.ndarray) -> float:
"""
Computes the fraction of correctly classified points
:param target: array of shape (n, ) with two possible values {-1, 1}
:param prediction: array of shape (n, ) with two possible values {-1, 1}
:return:
"""
num_correct_objects = np.sum(target == prediction)
return num_correct_objects / target.shape[0]
def precision_score(target: np.ndarray, prediction: np.ndarray) -> float:
"""
Computes the precision score tp / (tp + fp)
:param target: array of shape (n, ) with two possible values {-1, 1}
:param prediction: array of shape (n, ) with two possible values {-1, 1}
:return:
"""
tp = np.sum((prediction == 1) & (target == 1))
fp = np.sum((prediction == 1) & (target == 0))
return tp / (tp + fp)
def recall_score(target, prediction) -> float:
"""
Computes the recall metric: tp / (tp + fn)
:param target: array of shape (n, ) with two possible values {-1, 1}
:param prediction: array of shape (n, ) with two possible values {-1, 1}
:return:
"""
tp = np.sum((prediction == 1) & (target == 1))
fn = np.sum((prediction == 0) & (target == 1))
return tp / (tp + fn)
def F1_score(target: np.ndarray, prediction: np.ndarray) -> float:
"""
Computes the following combination of two metrics (2 * precision * recall) / (precision + recall)
:param target: array of shape (n, ) with two possible values {-1, 1}
:param prediction: array of shape (n, ) with two possible values {-1, 1}
:return:
"""
precision = precision_score(target, prediction)
recall = recall_score(target, prediction)
f1 = (2 * precision * recall) / (precision + recall)
return f1