how to compute AUCArea Under Curve for recommendation system evaluation
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In the realm of recommendation systems, evaluating the performance of algorithms is crucial to ensure users receive relevant and personalized content. Among various evaluation metrics, the Area Under the Receiver Operating Characteristic Curve (AUC-ROC) is a popular choice due to its ability to measure the overall performance of a model over all classification thresholds. This article delves into the computation of AUC for evaluating recommendation systems, providing technical insights and examples.
Understanding AUC
The AUC is a scalar value that represents the probability that a randomly chosen positive instance is ranked higher than a randomly chosen negative instance by the model. The value of AUC ranges from 0 to 1, with 0.5 indicating no discriminative ability (equivalent to random guessing), and 1.0 indicating perfect discrimination.
In the context of recommendation systems, items recommended to a user are ranked based on their predicted relevance scores. AUC provides a holistic measure of how well the model ranks relevant items higher than irrelevant ones.
Computing AUC: Step-by-Step Guide
Data Preparation
Suppose you have a dataset comprising the actual interaction labels (binary, where 1 indicates interaction and 0 indicates no interaction) and predicted scores from the model:
| Item | Actual Label | Predicted Score |
| I1 | 1 | 0.90 |
| I2 | 0 | 0.60 |
| I3 | 1 | 0.80 |
| I4 | 0 | 0.30 |
| I5 | 1 | 0.70 |
Step 1: Sort and Rank
Sort the items based on their predicted scores in descending order. This establishes the ranking of items by the model's confidence.
| Rank | Item | Actual Label | Predicted Score |
| 1 | I1 | 1 | 0.90 |
| 2 | I3 | 1 | 0.80 |
| 3 | I5 | 1 | 0.70 |
| 4 | I2 | 0 | 0.60 |
| 5 | I4 | 0 | 0.30 |
Step 2: Compute True Positive and False Positive Rates
Calculate the True Positive Rate (TPR) and False Positive Rate (FPR) for different thresholds by varying the cutoff point in the ranked list.
Threshold Calculation
Consider a threshold `t`, which separates positive from negative classes. For each threshold, calculate:
• True Positives (TP): Number of items with actual label 1 above the threshold. • False Positives (FP): Number of items with actual label 0 above the threshold. • True Negatives (TN): Number of items with actual label 0 below the threshold. • False Negatives (FN): Number of items with actual label 1 below the threshold.
TPR and FPR Formulas
Step 3: Plot ROC Curve
With TPR and FPR values computed for various thresholds, plot the ROC curve by placing FPR on the x-axis and TPR on the y-axis.
Step 4: Calculate AUC
AUC is the area under this ROC curve. Traditionally, this area is calculated using the trapezoidal rule.
In practice, AUC can be approximated by:
In programming contexts, libraries such as scikit-learn offer built-in functions to compute AUC, leveraging robust internal calculations that ensure efficiency and accuracy.
Related reading
- How to compute number of weights of CNN?
- How to compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn?
- How to compute the cosine_similarity in pytorch for all rows in a matrix with respect to all rows in another matrix
- How to compute the second derivatives diagonal of the Hessian in TensorFlow 2.0
- How to concatenate two layers in keras?
- How to concatenate two tensors horizontally in TensorFlow?
- How to construct a network with two inputs in PyTorch
- How to continue to train SVM based on the previous model
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.