Skip to content

← Metrics & Evaluation step 1 of 7

Easy Primitives

Accuracy – Multiclass

Accuracy is the fraction of examples assigned the correct class. When a model returns one score per class, choose the class with the largest score in each row. The index of that score is called the argmax.

For scores [[3, 1, 0], [0, 2, 1], [1, 0, 4]], the predicted classes are [0, 1, 2]. If the labels are [0, 2, 2], two of three predictions are correct, so accuracy is 2 / 3, approximately 0.666667.

Your task

Implement accuracy(predictions, labels):

  • predictions has shape (N, C), with N > 0 and C > 0. Entries are finite class scores, either logits or probabilities.
  • labels has shape (N,) and contains whole-number class IDs in [0, C - 1]. The test inputs may store these IDs as floating-point values.
  • Return a Python float between 0 and 1, not a percentage or a count.

Choose the highest-scoring class independently for each example. If scores tie for the maximum, choose the smallest class index. Compare these IDs with labels, count matches, and divide by N. Softmax is unnecessary: it preserves the ordering of scores within a row in exact arithmetic.

Accuracy counts every example equally and treats all mistakes alike. It can hide poor performance on a rare class: always predicting a class that occurs in 99% of examples gives 99% accuracy while missing every example outside it. Inspect per-class metrics or a confusion matrix when that distinction matters. Class balance alone does not decide which metric best reflects the task.