all been in that second, proper? Watching a chart as if it’s some historical script, questioning how we’re speculated to make sense of all of it. That’s precisely how I felt after I was requested to elucidate the AUC for the ROC curve at work lately.
Although I had a strong understanding of the mathematics behind it, breaking it down into easy, digestible phrases proved to be a problem. I spotted that if I used to be combating it, others most likely had been too. So, I made a decision to put in writing this text to share an intuitive option to perceive the AUC-ROC curve by a sensible instance. No dry definitions right here—simply clear, simple explanations targeted on the instinct.
Right here’s the code1 used on this article.
Each knowledge scientist goes by a section of evaluating classification fashions. Amidst an array of analysis metrics, Receiver Working Attribute (ROC) curve and the Area Under The Curve (AUC) is an indispensable device for gauging mannequin’s efficiency. On this complete article, we are going to talk about fundamental ideas and see them in motion utilizing our good outdated Titanic dataset2.
Part 1: ROC Curve
At its core, the ROC curve visually portrays the fragile stability between a mannequin’s sensitivity and specificity throughout various classification thresholds.
To totally grasp the ROC curve, let’s delve into the ideas:
- Sensitivity/Recall (True Optimistic Price): Sensitivity quantifies a mannequin’s adeptness at accurately figuring out constructive cases. In our Titanic instance, sensitivity corresponds to the the proportion of precise survival circumstances that the mannequin precisely labels as constructive.
- Specificity (True Detrimental Price): Specificity measures a mannequin’s proficiency in accurately figuring out adverse cases. For our dataset, it represents the proportion of precise non-survived circumstances (Survival = 0) that the mannequin accurately identifies as adverse.

- False Optimistic Price: FPR measures the proportion of adverse cases which can be incorrectly categorised as constructive by the mannequin.

Discover that Specificity and FPR are complementary to one another. Whereas specificity focuses on the right classification of adverse cases, FPR focuses on the inaccurate classification of adverse cases as constructive. Thus-

Now that we all know the definitions, let’s work with an instance. For Titanic dataset, I’ve constructed a easy logistic regression mannequin that predicts whether or not the passenger survived the shipwreck or not, utilizing following options: Passenger Class, Intercourse, # of siblings/spouses aboard, passenger fare and Port of Embarkation. Notice that, the mannequin predicts the ‘likelihood of survival’. The default threshold for logistic regression in sklearn is 0.5. Nevertheless, this default threshold could not at all times make sense for the issue being solved and we have to mess around with the likelihood threshold i.e. if the expected likelihood > threshold, occasion is predicted to be constructive else adverse.
Now, let’s revisit the definitions of Sensitivity, Specificity and FPR above. Since our predicted binary classification relies on the likelihood threshold, for the given mannequin, these three metrics will change primarily based on the likelihood threshold we use. If we use a better likelihood threshold, we are going to classify fewer circumstances as positives i.e. our true positives shall be fewer, leading to decrease Sensitivity/Recall. The next likelihood threshold additionally means fewer false positives, so low FPR. As such, growing sensitivity/recall might result in elevated FPR.
For our coaching knowledge, we are going to use 10 totally different likelihood cutoffs and calculate Sensitivity/TPR and FPR and plot in a chart beneath. Notice, the dimensions of circles within the scatterplot correspond to the likelihood threshold used for classification.

Effectively, that’s it. The graph we created above plots Sensitivity (TPR) Vs. FPR at numerous likelihood thresholds IS the ROC curve!
In our experiment, we used 10 totally different likelihood cutoffs with an increment of 0.1 giving us 10 observations. If we use a smaller increment for the likelihood threshold, we are going to find yourself with extra knowledge factors and the graph will appear to be our acquainted ROC curve.
To substantiate our understanding, for the mannequin we constructed for predicting passenger’s survival, we are going to loop by numerous predicted likelihood thresholds and calculate TPR, FPR for the testing dataset (see code snippet beneath). Plot the leads to a graph and examine this graph with the ROC curve plotted utilizing sklearn’s roc_curve
3 .

As we are able to see, the 2 curves are nearly equivalent. Notice the AUC=0.92 was calculated utilizing the roc_auc_score
4 perform. We’ll talk about this AUC within the later a part of this text.
To summarize, ROC curve plots TPR and FPR for the mannequin at numerous likelihood thresholds. Notice that, the precise chances are NOT displayed within the graph, however one can assume that the observations on the decrease left aspect of the curve correspond to increased likelihood thresholds (low TPR), and remark on the highest proper aspect correspond to decrease likelihood thresholds (excessive TPR).
To visualise what’s said above, check with the beneath chart, the place I’ve tried to annotate TPR and FPR at totally different likelihood cutoffs.

Part 2: AUC
Now that we’ve developed some instinct round what ROC curve is, the subsequent step is to grasp Space Below the Curve (AUC). However earlier than delving into the specifics, let’s take into consideration what an ideal classifier seems like. Within the preferrred case, we wish the mannequin to attain good separation between constructive and adverse observations. In different phrases, the mannequin assigns low chances to adverse observations and excessive chances to constructive observations with no overlap. Thus, there’ll exist some likelihood reduce off, such that each one observations with predicted likelihood < reduce off are adverse, and all observations with likelihood >= reduce off are constructive. When this occurs, True Optimistic Price shall be 1 and False Optimistic Price shall be 0. So the best state to attain is TPR=1 and FPR=0. In actuality, this doesn’t occur, and a extra sensible expectation ought to be to maximise TPR and decrease FPR.
Normally, as TPR will increase with decreasing likelihood threshold, the FPR additionally will increase (see chart 1). We would like TPR to be a lot increased than FPR. That is characterised by the ROC curve that’s bent in direction of the highest left aspect. The next ROC house chart reveals the right classifier with a blue circle (TPR=1 and FPR=0). Fashions that yield the ROC curve nearer to the blue circle are higher. Intuitively, it implies that the mannequin is ready to pretty separate adverse and constructive observations. Among the many ROC curves within the following chart, mild blue is finest adopted by inexperienced and orange. The dashed diagonal line represents random guesses (consider a coin flip).

Now that we perceive ROC curves skewed to the highest left are higher, how can we quantify this? Effectively, mathematically, this may be quantified by calculating the Space Below the Curve. The Space Below the Curve (AUC) of the ROC curve is at all times between 0 and 1 as a result of our ROC house is bounded between 0 and 1 on each axes. Among the many above ROC curves, the mannequin similar to the sunshine blue ROC curve is healthier in comparison with inexperienced and orange because it has increased AUC.
However how is AUC calculated? Computationally, AUC includes integrating the Roc curve. For fashions producing discrete predictions, AUC may be approximated utilizing the trapezoidal rule6. In its easiest type, the trapezoidal rule works by approximating the area below the graph as a trapezoid and calculating its space. I’ll most likely talk about this in one other article.
This brings us to the final and essentially the most awaited half — find out how to intuitively make sense of AUC? Let’s say you constructed a primary model of a classification mannequin with AUC 0.7 and also you later positive tune the mannequin. The revised mannequin has an AUC of 0.9. We perceive that the mannequin with increased AUC is healthier. However what does it actually imply? What does it suggest about our improved prediction energy? Why does it matter? Effectively, there’s quite a lot of literature explaining AUC and its interpretation. A few of them are too technical, some incomplete, and a few are outright fallacious! One interpretation that made essentially the most sense to me is:
AUC is the likelihood {that a} randomly chosen constructive occasion possesses a better predicted likelihood than a randomly chosen adverse occasion.
Let’s confirm this interpretation. For the straightforward logistic regression we constructed, we are going to visualize the expected chances of constructive and adverse lessons (i.e. Survived the shipwreck or not).

We are able to see the mannequin performs fairly nicely in assigning a better likelihood to Survived circumstances than people who didn’t. There’s some overlap of chances within the center part. The AUC calculated utilizing the auc rating
perform in sklearn for our mannequin on the check dataset is 0.92 (see chart 2). So primarily based on the above interpretation of AUC, if we randomly select a constructive occasion and a adverse occasion, the likelihood that the constructive occasion could have a better predicted likelihood than the adverse occasion ought to be ~92%.
For this function, we are going to create swimming pools of predicted chances of constructive and adverse outcomes. Now we randomly choose one remark every from each the swimming pools and examine their predicted chances. We repeat this 100K instances. Later we calculate % of instances the expected likelihood of a constructive occasion was > predicted likelihood of a adverse occasion. If our interpretation is appropriate, this ought to be equal to .

We did certainly get 0.92! Hope this helps.
Let me know your feedback and be at liberty to attach with me on LinkedIn.
Notice — this text is revised model of the original article that I wrote on Medium in 2023.
References:
- https://github.com/Swpnilsp/ROC-AUC-Curve/blob/main/RoC_Curve_Analysis%20(2).ipynb
- https://www.kaggle.com/competitions/titanic/data (License-CC0: Public Domain)
- https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html#sklearn.metrics.roc_curve
- https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html
- https://en.wikipedia.org/wiki/Receiver_operating_characteristic
- https://en.wikipedia.org/wiki/Trapezoidal_rule