-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification_scores.py
More file actions
114 lines (61 loc) · 2.75 KB
/
Copy pathclassification_scores.py
File metadata and controls
114 lines (61 loc) · 2.75 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
import numpy as np
from sklearn.metrics import confusion_matrix
def get_unweighed_average_recall(y_true, y_pred):
'''
Recall true_positive / (true_positive + false_negative)
This function implements the unweighed version of recall,
meaning that all classes have the same weigh in the calculation.
'''
cm = confusion_matrix(y_true, y_pred).astype(float)
#calculate the relative (class specific) count of true positives (diagonal) and average over all classes
for i in range(len(cm)):
row_sum = np.sum(cm[i, :])
cm[i, :] = np.divide(cm[i, :], row_sum)
return {"uar": np.mean(np.diagonal(cm))}
def get_recall(y_true, y_pred):
cm = confusion_matrix(y_true, y_pred)
tp_total = np.sum(np.diagonal(cm))
fn_total = 0
for i in range(len(cm)):
row = cm[i, :]
row[i] = 0
fn_total = fn_total + np.sum(row)
return {"recall": (tp_total/(tp_total+fn_total))}
def get_unweighed_average_precision(y_true, y_pred):
'''
precision true_positive / (true_positive + false_positive)
This function implements the unweighed version of precision,
meaning that all classes have the same weigh in the calculation.
'''
cm = confusion_matrix(y_true, y_pred).astype(float)
#calculate the relative (class specific) count of true positives (diagonal) and average over all classes
for i in range(len(cm)):
col_sum = np.sum(cm[:, i])
cm[i, :] = np.divide(cm[:, i], col_sum)
return {"uap": np.mean(np.diagonal(cm))}
def get_precision(y_true, y_pred):
cm = confusion_matrix(y_true, y_pred)
tp_total = np.sum(np.diagonal(cm))
fp_total = 0
for i in range(len(cm)):
column = cm[:, i]
column[i] = 0
fp_total = fp_total + np.sum(column)
return {"precision": (tp_total/(tp_total+fp_total))}
def get_accuracy(y_true, y_pred):
'''
Accuracy true_positive / all_predictions
'''
cm = confusion_matrix(y_true, y_pred)
return {"accuracy": (np.sum(np.diagonal(cm))/np.sum(cm))}
def get_f1_score(y_true, y_pred):
recall = get_recall(y_true, y_pred)["recall"]
precision = get_precision(y_true, y_pred)["precision"]
return {"f1": (recall+precision)/2}
def get_all_scores(y_true, y_pred):
return (get_unweighed_average_recall(y_true, y_pred),
get_unweighed_average_precision(y_true, y_pred),
get_precision(y_true, y_pred),
get_recall(y_true, y_pred),
get_accuracy(y_true, y_pred),
get_f1_score(y_true, y_pred))