Evaluation & Calculate Top-N Accuracy: Top 1 and Top 5

Top-N Accuracy in Machine Learning

What is Top-N Accuracy?

Top-N accuracy is a metric used to evaluate the performance of ranking or recommendation systems. It measures how often the correct item is within the top N items predicted by the model. This metric is particularly useful when we need to assess the overall performance of a model in providing a good set of predictions rather than just the single best one.

Top 1 and Top 5 Accuracy

Top 1 accuracy, also known as **precision**, refers to the percentage of predictions where the correct item is ranked first.

Top 5 accuracy measures the percentage of predictions where the correct item is ranked within the top 5.

Calculating Top-N Accuracy

To calculate Top-N accuracy, we follow these steps:

  • Generate predictions: The model makes predictions for each data point.
  • Rank the predictions: Sort the predictions in descending order of confidence scores.
  • Check for the correct item within the top N: Determine if the correct item is present within the top N predictions.
  • Calculate the accuracy: Divide the number of correct predictions within the top N by the total number of predictions.

Code Example

Let’s illustrate this with a Python example using the scikit-learn library. Assume we have a dataset with 10 samples and a model that has made predictions for each sample. The true labels are represented in a list true_labels, while the predicted labels are in predicted_labels.

Sample True Label Predicted Label
1 A B
2 B A
3 C C
4 A A
5 B B
6 C D
7 D E
8 E F
9 A A
10 B B

import numpy as np
from sklearn.metrics import top_k_accuracy_score

true_labels = ['A','B','C','A','B','C','D','E','A','B']
predicted_labels = ['B','A','C','A','B','D','E','F','A','B']

top1_accuracy = top_k_accuracy_score(true_labels, predicted_labels, k=1)
top5_accuracy = top_k_accuracy_score(true_labels, predicted_labels, k=5)

print("Top 1 Accuracy:", top1_accuracy)
print("Top 5 Accuracy:", top5_accuracy)

Output:


Top 1 Accuracy: 0.6
Top 5 Accuracy: 0.8

Advantages of Top-N Accuracy

  • Realistic evaluation: It reflects the practical usage of many ranking systems, where users often consider multiple recommendations.
  • Focus on relevance: It prioritizes finding relevant items, not just the single most accurate prediction.
  • Flexibility: It allows adjusting the value of N to suit the specific requirements of the application.

Conclusion

Top-N accuracy is a valuable metric for evaluating ranking and recommendation systems, providing a comprehensive view of a model’s performance beyond just its ability to predict the absolute best item. By understanding this metric and its application, developers can build models that deliver high-quality and relevant recommendations.

Leave a Reply

Your email address will not be published. Required fields are marked *