Fast Computation of AUC-ROC score
JeanFrancoisPuget 2700028FGP Visits (8477)
Area under ROC curve (AUC-ROC) is one of the most common evaluation metric for binary classification problems. We show here a simple and very efficient way to compute it with Python. Before showing the code, let's briefly describe what an evaluation metric is, and what AUC-ROC is in particular.
An evaluation metric is a way to assess how good a machine learning model is. It is used to compute one or more numbers that summarize how the machine learning model predictions compare to reality. In order to use an evaluation metric, one has to go thought these steps:
When the target only takes two values we have a binary classification problem at hand. Example of binary classification are very common. For instance fraud detection where examples are credit card transactions, features are time, location, amount, merchant id, etc., and target is fraud or not fraud. Spam detection is also a binary classification where examples are emails, features are the email content as a string of words, and target is spam or not spam. Without loss of generality we can assume that the target values are 0 and 1, for instance 0 means no fraud or no spam, while 1 means fraud or spam.
For binary classification, predictions are also binary. Therefore, a prediction is either equal to the target, or is off the mark. A simple way to evaluate model performance is accuracy: how many predictions are right? For instance, if our test set has 100 examples in it, how many times is the prediction correct? Accuracy seems a logical way to evaluate performance: a higher accuracy obviously means a better model. At least this is what people think when they are exposed to the first time to binary classification problems. Issue is that accuracy can be extremely misleading.
Let's see why. Assume I have a binary classification problem, for instance fraud detection, and that I have a model with 99% accuracy. My model predicts the correct target correctly for 99 of the examples in the test set. It looks like I got a near perfect model, isn't it?
Well, what if reality is the following?
If you look at it, my model is correct 99% of the time. Yet it is absolutely useless.
In order to cope with this issue several alternative metrics have been proposed to replace accuracy, like precision, recall, F1 score. But these metrics, as well as accuracy, do not apply to many interesting and effective algorithms. These are algorithms that output a probability rather than a binary value. A probability close to 0 means that the algorithm thinks the target is 0, while a probability close to 1 means that the algorithm thinks the target is 1. Algorithms in this class include logistic regression, gradient boosted trees with log loss, and neural networks with cross entropy loss. One way to use these algorithm is to threshold their output: a probability under 0.5 is transformed into a 0, and a value above 0.5 is transformed into a 1. After thresholding any of the above metric can be used.
We used 0.5 as the threshold, but we could have used any other value between 0 and 1. A conservative value would be to use a threshold close to 0, for instance 0.1. This amounts to classify as non fraud or non spam only the examples that the algorithm is very confident about. And of course, depending on the threshold you use the evaluation metric will yield different values.
It would be nice to be able to evaluate the performance of a model without the need to select an arbitrary threshold. This is precisely what AUC-ROC is providing. I'll refer to wikipedia for the classical way of defining that metric. I will use a much simpler way here.
Let's first define some entities.
Then AUC-ROC is the count of pairs labelled the right way divided by the number of pairs:
A naive code to compute this would be to consider each possible pair and count those labelled the right way. A much better way is to sort the predictions first, then visit the examples in increasing order of predictions. Each time we see a positive example we add the number of negative examples we've seen so far. We use the numba compiler to make it run fast:
import numpy as np from numba import jit @jit def fast_auc(y_true, y_prob): y_true = np.asarray(y_true) y_true = y_tr
On my macbook pro it runs about twice as fast as the corresponding sckit-learn function. A notebook with the code and a benchmark is available on github.
Edited on Nov 14, 2017. If you are looking for a fast AUC-ROC code in R then have a look at Ben Gorman's code.