all been in that second, proper? Watching a chart as if it’s some historical script, questioning how we’re purported to make sense of all of it. That’s precisely how I felt once I was requested to clarify the AUC for the ROC curve at work lately.
Although I had a stable understanding of the maths behind it, breaking it down into easy, digestible phrases proved to be a problem. I noticed that if I used to be battling it, others in all probability had been too. So, I made a decision to write down this text to share an intuitive solution to perceive the AUC-ROC curve by way of a sensible instance. No dry definitions right here—simply clear, easy explanations targeted on the instinct.
Right here’s the code1 used on this article.
Each knowledge scientist goes by way of 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 focus on primary 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 Fee): Sensitivity quantifies a mannequin’s adeptness at appropriately figuring out optimistic situations. In our Titanic instance, sensitivity corresponds to the the proportion of precise survival circumstances that the mannequin precisely labels as optimistic.
- Specificity (True Unfavourable Fee): Specificity measures a mannequin’s proficiency in appropriately figuring out unfavourable situations. For our dataset, it represents the proportion of precise non-survived circumstances (Survival = 0) that the mannequin appropriately identifies as unfavourable.

- False Optimistic Fee: FPR measures the proportion of unfavourable situations which are incorrectly labeled as optimistic by the mannequin.

Discover that Specificity and FPR are complementary to one another. Whereas specificity focuses on the right classification of unfavourable situations, FPR focuses on the inaccurate classification of unfavourable situations as optimistic. 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. Be aware that, the mannequin predicts the ‘likelihood of survival’. The default threshold for logistic regression in sklearn is 0.5. Nevertheless, this default threshold might 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 optimistic else unfavourable.
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 will probably be fewer, leading to decrease Sensitivity/Recall. The next likelihood threshold additionally means fewer false positives, so low FPR. As such, rising sensitivity/recall might result in elevated FPR.
For our coaching knowledge, we are going to use 10 completely different likelihood cutoffs and calculate Sensitivity/TPR and FPR and plot in a chart beneath. Be aware, 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 completely 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 like our acquainted ROC curve.
To verify our understanding, for the mannequin we constructed for predicting passenger’s survival, we are going to loop by way of numerous predicted likelihood thresholds and calculate TPR, FPR for the testing dataset (see code snippet beneath). Plot the leads to a graph and evaluate 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. Be aware the AUC=0.92 was calculated utilizing the roc_auc_score
4 perform. We are going to focus on 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. Be aware that, the precise chances are NOT displayed within the graph, however one can assume that the observations on the decrease left facet of the curve correspond to increased likelihood thresholds (low TPR), and statement on the highest proper facet correspond to decrease likelihood thresholds (excessive TPR).
To visualise what’s acknowledged above, check with the beneath chart, the place I’ve tried to annotate TPR and FPR at completely different likelihood cutoffs.

Part 2: AUC
Now that we’ve got developed some instinct round what ROC curve is, the subsequent step is to know Space Underneath the Curve (AUC). However earlier than delving into the specifics, let’s take into consideration what an ideal classifier seems to be like. Within the preferrred case, we wish the mannequin to realize good separation between optimistic and unfavourable observations. In different phrases, the mannequin assigns low chances to unfavourable observations and excessive chances to optimistic observations with no overlap. Thus, there’ll exist some likelihood minimize off, such that every one observations with predicted likelihood = minimize off are optimistic. When this occurs, True Optimistic Fee will probably be 1 and False Optimistic Fee will probably be 0. So the best state to realize 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.
On the whole, 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 facet. The next ROC area chart exhibits 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 signifies that the mannequin is ready to pretty separate unfavourable and optimistic 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 will we quantify this? Effectively, mathematically, this may be quantified by calculating the Space Underneath the Curve. The Space Underneath the Curve (AUC) of the ROC curve is at all times between 0 and 1 as a result of our ROC area 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 best 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 kind, the trapezoidal rule works by approximating the area below the graph as a trapezoid and calculating its space. I’ll in all probability focus on this in one other article.
This brings us to the final and essentially the most awaited half — the right way 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 nice tune the mannequin. The revised mannequin has an AUC of 0.9. We perceive that the mannequin with increased AUC is best. However what does it actually imply? What does it suggest about our improved prediction energy? Why does it matter? Effectively, there’s lots of literature explaining AUC and its interpretation. A few of them are too technical, some incomplete, and a few are outright incorrect! One interpretation that made essentially the most sense to me is:
AUC is the likelihood {that a} randomly chosen optimistic occasion possesses a better predicted likelihood than a randomly chosen unfavourable occasion.
Let’s confirm this interpretation. For the straightforward logistic regression we constructed, we are going to visualize the expected chances of optimistic and unfavourable lessons (i.e. Survived the shipwreck or not).

We will see the mannequin performs fairly nicely in assigning a better likelihood to Survived circumstances than those 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 optimistic occasion and a unfavourable occasion, the likelihood that the optimistic occasion may have a better predicted likelihood than the unfavourable occasion ought to be ~92%.
For this objective, we are going to create swimming pools of predicted chances of optimistic and unfavourable outcomes. Now we randomly choose one statement every from each the swimming pools and evaluate their predicted chances. We repeat this 100K instances. Later we calculate % of instances the expected likelihood of a optimistic occasion was > predicted likelihood of a unfavourable 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 happy to attach with me on LinkedIn.
Be aware — 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