From 80b0bfe3de7217b304c5bd916f83c62ea648effd Mon Sep 17 00:00:00 2001 From: Mathieu Blondel Date: Fri, 2 Aug 2013 13:48:39 +0900 Subject: [PATCH] Completely avoid for loop in _auc. --- sklearn/metrics/tests/test_metrics.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/sklearn/metrics/tests/test_metrics.py b/sklearn/metrics/tests/test_metrics.py index 98d84bba1d321..1d83fa214e505 100644 --- a/sklearn/metrics/tests/test_metrics.py +++ b/sklearn/metrics/tests/test_metrics.py @@ -303,16 +303,13 @@ def _auc(y_true, y_score): n_samples = y_true.shape[0] ind = np.arange(n_samples) pos_label = np.unique(y_true)[1] - pos = ind[y_true == pos_label] - neg = ind[y_true != pos_label] # Count the number of times positive samples are correctly ranked above # negative samples. - n_correct = 0 - for i in pos: - for j in neg: - if y_score[i] > y_score[j]: - n_correct += 1 + pos = y_score[y_true == pos_label] + neg = y_score[y_true != pos_label] + diff_matrix = pos.reshape(1, -1) - neg.reshape(-1, 1) + n_correct = np.sum(diff_matrix > 0) return n_correct / float(len(pos) * len(neg))