Notes

Top-k Accuracy

When a model has to choose among many possible classes, it’s not always fair to judge it only on whether its single top guess was perfect. Top-k accuracy captures a more forgiving (and often more realistic) view: “Did the right answer appear anywhere in the model’s first k guesses?”

What it measures

Top-k accuracy is the fraction of examples where the true class label is included in the model’s k highest-scoring predicted classes. If a classifier outputs a probability (or score) for each class, you sort classes by that score and check whether the correct class is in the first k. When k = 1, top-k accuracy is just ordinary accuracy.

Why it matters in practice

Top-k accuracy is especially useful in multi-class problems with lots of classes or ambiguous labels, where near-misses are still valuable:

  • Image classification (e.g., 1,000 ImageNet categories): top-5 accuracy is a standard headline metric.
  • Medical coding or diagnosis suggestion: a clinician may review a shortlist of likely codes/conditions.
  • Customer support ticket routing: showing agents the top 3 suggested categories can speed triage even if the top 1 is wrong.

If you ignore it and only report top-1 accuracy, you can underestimate a model that’s consistently “almost right,” which matters when the downstream system can use a ranked list.

How it’s computed (and a library you’ll see)

Given predicted scores per class, compute the arg top-k classes and count hits. In scikit-learn, you’ll commonly use sklearn.metrics.top_k_accuracy_score with predicted probabilities from models like LogisticRegression (multinomial) or a neural net.

Top-k Accuracy is a classification metric that counts a prediction as correct if the true class label appears among the model’s k highest-scored (most probable) classes, rather than requiring it to be the single top prediction. It matters when multiple plausible classes exist or when downstream systems can consider several candidates (e.g., top-5 in ImageNet), providing a more informative view of ranking quality than standard accuracy.

Imagine a trivia game where you don’t have to give the single best answer—you can name your top few guesses. If the correct answer is anywhere in your list, you still get credit. Top-k Accuracy works the same way for AI that chooses between categories (like “cat,” “dog,” “rabbit”). Instead of asking, “Was the model’s #1 pick right?”, it asks, “Was the right label in the model’s top k picks?” For example, top-5 accuracy counts a prediction as correct if the true class appears in the model’s five most likely options. This is useful when several answers are plausible.