This article will introduce in detail the offline evaluation indicators commonly used in recommendation systems, including precision, recall, accuracy, F1-Score, NDCG, hit rate, AUC, GAUC and log loss. These metrics are crucial for evaluating the performance and effectiveness of recommendation systems. These metrics are crucial for evaluating the performance and effectiveness of recommendation systems.
In a recommendation system, the precision rate measures the proportion of items in the recommendation list that truly match the user's interests, and the recall rate measures the proportion of all items that match the user's interests that are successfully recommended. Accuracy measures the proportion of model predictions that are consistent with actual results. F1-Score takes precision and recall into consideration to comprehensively evaluate the model. NDCG is used to evaluate the ranking quality of recommendation systems, and is especially suitable for recommendation systems that consider element correlation ranking.
We will provide detailed explanations and calculation formulas for each indicator, and give sample code for Python implementation. The suitability of these metrics will be evaluated based on the needs of the recommendation system to help you choose the metrics that are suitable for your own system evaluation.
By gaining an in-depth understanding of these offline evaluation metrics, you will be able to better evaluate and improve your recommendation system and provide more accurate and personalized recommendation services.
1. Precision
Function
Precision is used to measure the accuracy of a classification model, that is, the proportion of samples predicted as positive by the model that are actually positive. In recommendation systems, accuracy can be understood as the proportion of items that truly match the user's interests among the recommended items received by the user.
Calculation formula
In the two-classification problem, the calculation formula of accuracy is:
$$ Precision = \frac{TP}{TP+FP} $$
Among them, TP stands for True Positives and FP stands for False Positives.
Example
Suppose a recommendation system recommends 10 products, 5 of which are of real interest to the user. Then the accuracy is: 5 / 10 = 0.5.
Python implementation
1 | from sklearn.metrics import precision_score |
Applicability
⭐️⭐️⭐️⭐️⭐️
Precision is very suitable for the evaluation of recommender systems. The main goal of a recommendation system is to accurately recommend products or services that may be of interest to users. If most of the recommended products are of interest to the user, the accuracy will be high.
2. Recall
Function
Recall is used to measure the ability of a classification model to cover positive class samples, that is, among all samples that are actually positive class, the model predicts the proportion of positive class. In recommendation systems, recall rate can be understood as the proportion of items that are successfully recommended by the model among all items that match the user's interests.
Calculation formula
In the binary classification problem, the calculation formula of recall rate is:
$$ Recall = \frac{TP}{TP+FN} $$
Among them, TP stands for True Positives and FN stands for False Negatives.
Example
Assume that there are 20 products that a user is really interested in, and the recommendation system successfully recommends 10 of them. Then the recall rate is: 10 / 20 = 0.5.
Python implementation
1 | from sklearn.metrics import recall_score |
Applicability
⭐️⭐️⭐️⭐️⭐️
Recall is also very suitable for the evaluation of recommender systems. One of the goals of a recommendation system is to cover as many products or services that users are interested in as possible. If most of the products that users are interested in can be recommended, the recall rate will be high.
3. Accuracy
Function
Accuracy is used to measure the proportion of the prediction results of the classification model that are consistent with the actual results, that is, the proportion of the model predictions that are correct among all samples.
Calculation formula
In the two-classification problem, the calculation formula of accuracy is:
$$ Accuracy = \frac{TP+TN}{TP+FP+TN+FN} $$
Among them, TP stands for True Positives, TN stands for True Negatives, FP stands for False Positives, and FN stands for False Negatives.
Example
Suppose a recommendation system makes predictions for 100 items, and 70 of the predictions are correct, then the accuracy rate is: 70 / 100 = 0.7.
Python implementation
1 | from sklearn.metrics import accuracy_score |
Applicability
⭐️⭐️⭐️
Accuracy has low applicability in recommender systems because recommender systems often face the problem of unbalanced labels. For example, in a product recommendation system, users may not be interested in most products, so the accuracy cannot well reflect the performance of the model.
4. F1-Score
Function
F1-Score is the harmonic average of precision and recall, which is used to comprehensively evaluate the model by considering precision and recall at the same time.
Calculation formula
The calculation formula of F1-Score is:
$$ F1-Score = 2 \times \frac{Precision \times Recall}{Precision + Recall} $$
Example
Assuming that the precision rate of a recommendation system is 0.5 and the recall rate is 0.7, then the F1-Score is: 2 * (0.5 * 0.7) / (0.5 + 0.7) = 0.583.
Python implementation
1 | from sklearn.metrics import f1_score |
Applicability
⭐️⭐️⭐️⭐️⭐️
F1-Score is very suitable for the evaluation of recommender systems. The recommendation system needs to balance precision and recall. A high precision indicates the accuracy of the recommendation, and a high recall indicates the comprehensiveness of the recommendation. F1-Score is an evaluation index that takes into account both.
5. NDCG (Normalized Discounted Cumulative Gain)
Function
NDCG is an index used to evaluate the ranking quality of recommendation systems, especially for those that consider element relevance ranking. It measures how similar the model's predicted ranked list is to the true ranked list.
Calculation formula
The calculation formula of NDCG is:
$$ NDCG = \frac{DCG}{IDCG} $$
Among them, DCG represents the Discounted Cumulative Gain of the recommendation list, and the calculation formula is:
$$ DCG = \sum_{i=1}^{N} \frac{2^{rel_i} - 1}{log_2(i + 1)} $$
IDCG represents the maximum DCG under ideal conditions, that is, all relevant products are ranked first. The calculation formula is the same as DCG, except that the products are sorted from large to small according to their relevance.
Example
Suppose a recommendation system predicts the relevance of 5 items as [3, 2, 3, 0, 1], then DCG = (2^3 - 1)/log2(1+1) + (2^2 - 1)/log2(2+1) + (2^3 - 1)/log2(3+1) + (2^0 - 1)/log2(4+1) + (2^1 - 1)/log2(5+1). If the ideal ordering is [3, 3, 2, 1, 0], then IDCG can be calculated in the same way, NDCG = DCG / IDCG.
Python implementation
1 | from sklearn.metrics import ndcg_score |
Applicability
⭐️⭐️⭐️⭐️⭐️
NDCG is well suited for the evaluation of recommender systems, especially for systems where recommendation ranking needs to be considered. NDCG can measure the similarity between the ranked list predicted by the recommendation system and the real ranked list, thereby evaluating the performance of the model.
Due to space limitations, I will discuss the following indicators (Hit Rate, AUC, GAUC, LogLoss) next time
Continue the introduction in the reply.
6. Hit Rate
Function
The hit rate is an indicator to evaluate whether the recommendation system can recommend items that the user is interested in. It can be understood as whether the system "hits" the items that the user is interested in.
Calculation formula
The formula for calculating hit rate is:
$$ HitRate = \frac{Number\ of\ Hits}{Total\ Number\ of\ Tests} $$
Example
Suppose a recommendation system makes predictions for 10 products, and 3 of them are hit, then the hit rate is: 3 / 10 = 0.3.
Python implementation
1 | def hit_rate(recommended_items, true_items): |
Applicability
⭐️⭐️⭐️⭐️⭐️
Hit rate is very important for the evaluation of recommendation systems. Because the main goal of the recommendation system is to recommend items that the user is interested in, if the recommended items include items that the user is really interested in, the hit rate will be high.
Thank you very much for your suggestions. Below is an introduction to the modified AUC and GAUC based on your suggestions.
7. AUC (Area Under Curve)
Function
AUC is a commonly used performance evaluation index for classification problems, especially for recommendation systems. It is an index that measures the model's discrimination between positive and negative samples. For each positive sample, calculate the proportion of negative samples whose prediction score is higher than that of the positive sample, that is, the "positive sample rate" of the positive sample. AUC is the average positive sample rate of all positive samples.
Calculation formula
The calculation of AUC can be described as:
- For each user, the recommendation system’s prediction scores for positive and negative samples are calculated.
- For each positive sample, calculate the proportion of negative samples its prediction score is higher than, that is, the "positive sample rate" of the positive sample.
- Calculate the average positive sample rate of all positive samples, that is, AUC.
The specific formula can be expressed as:
$$ AUC = \frac{1}{M}\sum_{i=1}^{M} \frac{1}{P_iN_i} \sum_{j=1}^{P_i} \sum_{k=1}^{N_i} I(s_{ij} > s_{ik}) $$
Among them, $M$ is the number of users, $P_i$ and $N_i$ are the number of positive samples and negative samples of user $i$ respectively, $s_{ij}$ and $s_{ik}$ are the prediction scores of positive sample $j$ and negative sample $k$ of user $i$ respectively, $I(\cdot)$ is the indicator function.
Python implementation
1 | from sklearn.metrics import roc_auc_score |
In particular, it is important to note that interview questions that require handwritten AUC calculations in Python are often encountered during interviews. An example is shown below, which mainly considers two-classification problems.
1 | def calculate_auc_manual(y_true, y_score): |
Applicability
⭐️⭐️⭐️⭐️⭐️
GAUC is also well suited for the evaluation of recommender systems, especially those where individual differences need to be taken into account. By calculating each user's AUC and averaging it, GAUC can more fully reflect the performance of the recommendation system.
Applicability
⭐️⭐️⭐️⭐️⭐️
GAUC is also well suited for the evaluation of recommender systems, especially those where individual differences need to be taken into account. By calculating each user's AUC and averaging it, GAUC can more fully reflect the performance of the recommendation system.
9. LogLoss (Logarithmic Loss)
Function
LogLoss is a measure of the loss function of a classification model that takes into account the probability value predicted by the model. For binary classification problems, the smaller the value, the better the performance of the model.
Calculation formula
The calculation formula of LogLoss is:
$$ LogLoss = -\frac{1}{n}\sum_{i=1}^{n}[y_i\log(p_i) + (1 - y_i)\log(1 - p_i)] $$
Example
Assume that the predicted probabilities of a recommendation system for three samples are [0.8, 0.6, 0.3], and the real labels of these three samples are [1, 1, 0], then LogLoss can be calculated by substituting the formula.
Python implementation
1 | from sklearn.metrics import log_loss |
Applicability
⭐️⭐️⭐️⭐️
LogLoss is suitable in the evaluation of recommendation systems, but it may not be the most important metric. LogLoss pays more attention to whether the probability value predicted by the model is accurate, while the recommendation system also needs to consider other factors, such as coverage, novelty, etc., in addition to the accuracy of prediction. Therefore, LogLoss can be used as an auxiliary indicator to measure recommendation systems.