-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetrics.py
224 lines (167 loc) · 8.02 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
class Metrics:
def __init__(self):
pass
def accuracy(self, y_true, y_pred):
"""
Function that computes the accuracy on a multiclass-multilabel problem
:arg
self (Metrics): instance of the class
y_true (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the true pathology
y_pred (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the predicted pathology
:return
mean_accuracy (float): mean accuracy of all the prediction done
accuracy_class (list): accuracy of each class
"""
if y_true.ndim == 1:
y_true = y_true[:, np.newaxis]
if y_pred.ndim == 1:
y_pred = y_pred[:, np.newaxis]
accuracy_class = []
for i in range(y_true.shape[1]):
accuracy_class.append(accuracy_score(y_true[:, i], y_pred[:, i]))
y_true = y_true.flatten(order='C')
y_pred = y_pred.flatten(order='C')
mean_accuracy = accuracy_score(y_true, y_pred)
return mean_accuracy, accuracy_class
def roc_metrics(self, y_true, y_proba):
"""
Function that compute the false and true positive rates
:arg
self (Metrics): instance of the class
y_true (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the true pathology
y_proba (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the predicted probability pathology
:return
fpr (numpy array): false positive rate
tpr (numpy array): true positive rate
"""
y_true = y_true.flatten(order='C')
y_proba = y_proba.flatten(order='C')
fpr, tpr, _ = roc_curve(y_true, y_proba)
return fpr, tpr
def precision_recall(self, y_true, y_proba):
"""
Function that compute the precision and recall
:arg
self (Metrics): instance of the class
y_true (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the true pathology
y_proba (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the predicted probability pathology
:return
precision (numpy array): false positive rate
recall (numpy array): true positive rate
"""
y_true = y_true.flatten(order='C')
y_proba = y_proba.flatten(order='C')
precision, recall, _ = precision_recall_curve(y_true, y_proba)
return precision, recall
def cohen_kappa_score(self, y_true, y_pred):
"""
Function that compute the kappa cohen score on a multiclass-multilabel problem
:arg
self (Metrics): instance of the class
y_true (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the true pathology
y_pred (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the predicted pathology
:return
mean_kappa_score (float): mean kappa cohen score of all the prediction done
kappa_class (list): kappa cohen score of each class
"""
if y_true.ndim == 1:
y_true = y_true[:, np.newaxis]
if y_pred.ndim == 1:
y_pred = y_pred[:, np.newaxis]
kappa_class = []
for i in range(y_true.shape[1]):
kappa_class.append(cohen_kappa_score(y_true[:, i], y_pred[:, i]))
y_true = y_true.flatten(order='C')
y_pred = y_pred.flatten(order='C')
mean_kappa_score = cohen_kappa_score(y_true, y_pred)
return mean_kappa_score, kappa_class
def f1_score(self, y_true, y_pred):
"""
Function that compute the f1 score on a multiclass-multilabel problem
:arg
self (Metrics): instance of the class
y_true (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the true pathology
y_pred (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the predicted pathology
:return
mean_f1 (float): mean f1 score of all the prediction done
f1_class (list): f1 score score of each class
"""
if y_true.ndim == 1:
y_true = y_true[:, np.newaxis]
if y_pred.ndim == 1:
y_pred = y_pred[:, np.newaxis]
f1_class = []
for i in range(y_true.shape[1]):
f1_class.append(f1_score(y_true[:, i], y_pred[:, i]))
y_true = y_true.flatten(order='C')
y_pred = y_pred.flatten(order='C')
mean_f1 = f1_score(y_true, y_pred)
return mean_f1, f1_class
def precision(self, y_true, y_pred):
"""
Function that compute the precision score on a multiclass-multilabel problem
:arg
self (Metrics): instance of the class
y_true (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the true pathology
y_pred (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the predicted pathology
:return
mean_precision (float): mean precision score of all the prediction done
precision_class (list): precision score score of each class
"""
if y_true.ndim == 1:
y_true = y_true[:, np.newaxis]
if y_pred.ndim == 1:
y_pred = y_pred[:, np.newaxis]
precision_class = []
for i in range(y_true.shape[1]):
precision_class.append(precision_score(y_true[:, i], y_pred[:, i]))
y_true = y_true.flatten(order='C')
y_pred = y_pred.flatten(order='C')
mean_precision = precision_score(y_true, y_pred)
return mean_precision, precision_class
def recall(self, y_true, y_pred):
"""
Function that compute the recall score on a multiclass-multilabel problem
:arg
self (Metrics): instance of the class
y_true (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the true pathology
y_pred (numpy array): 1D or 2D numpy array where each rows correspond to an image and each column correspond
to the predicted pathology
:return
mean_recall (float): mean recall score of all the prediction done
recall_class (list): recall score score of each class
"""
if y_true.ndim == 1:
y_true = y_true[:, np.newaxis]
if y_pred.ndim == 1:
y_pred = y_pred[:, np.newaxis]
recall_class = []
for i in range(y_true.shape[1]):
recall_class.append(recall_score(y_true[:, i], y_pred[:, i]))
y_true = y_true.flatten(order='C')
y_pred = y_pred.flatten(order='C')
mean_recall = recall_score(y_true, y_pred)
return mean_recall, recall_class