Skip to content

Commit 80b0bfe

Browse files
committed
Completely avoid for loop in _auc.
1 parent 053723c commit 80b0bfe

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

sklearn/metrics/tests/test_metrics.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,13 @@ def _auc(y_true, y_score):
303303
n_samples = y_true.shape[0]
304304
ind = np.arange(n_samples)
305305
pos_label = np.unique(y_true)[1]
306-
pos = ind[y_true == pos_label]
307-
neg = ind[y_true != pos_label]
308306

309307
# Count the number of times positive samples are correctly ranked above
310308
# negative samples.
311-
n_correct = 0
312-
for i in pos:
313-
for j in neg:
314-
if y_score[i] > y_score[j]:
315-
n_correct += 1
309+
pos = y_score[y_true == pos_label]
310+
neg = y_score[y_true != pos_label]
311+
diff_matrix = pos.reshape(1, -1) - neg.reshape(-1, 1)
312+
n_correct = np.sum(diff_matrix > 0)
316313

317314
return n_correct / float(len(pos) * len(neg))
318315

0 commit comments

Comments
 (0)