How to calculate multiclass overall accuracy, sensitivity and specificity?
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
Overall accuracy in multiclass classification is straightforward, but sensitivity and specificity are not single direct formulas the way they are in a binary problem. The standard approach is to compute them one class at a time using a one-versus-rest view, then decide explicitly how to average those class-wise values.
Start With the Confusion Matrix
For multiclass metrics, the confusion matrix is the cleanest source of truth. Accuracy is simply the sum of the diagonal divided by the total number of samples.
Accuracy is easy because it ignores the class-by-class tradeoffs. Sensitivity and specificity require more structure.
Compute Sensitivity and Specificity Per Class
For a given class i, pretend that class is the positive class and all other classes are negative. Then derive:
- true positive as
cm[i, i] - false negative as the rest of row
i - false positive as the rest of column
i - true negative as everything else
That gives the binary-style formulas:
- sensitivity =
TP / (TP + FN) - specificity =
TN / (TN + FP)
This is the key step most explanations skip. There is no single native multiclass sensitivity without first deciding how class-wise sensitivity should be combined.
Choose the Averaging Rule Explicitly
Once you have per-class values, you need to choose how to summarize them. The common options are:
- macro average: plain mean across classes
- weighted average: mean weighted by class support
- micro-style pooling: aggregate counts first, then compute ratios
Macro averaging treats each class equally. Weighted averaging gives bigger classes more influence. Neither is "the" correct answer in all settings. The right one depends on whether rare classes matter as much as common ones.
Why Accuracy Alone Is Not Enough
In imbalanced problems, accuracy can look healthy even when one class is being ignored. Per-class sensitivity exposes whether the model is actually finding the positives for each class. Specificity tells you whether the model is incorrectly assigning other classes to that class too often.
One subtle point: specificity can look artificially high in multiclass settings because each one-versus-rest comparison includes many true negatives. That is why reporting only one high specificity number can be misleading without the accompanying sensitivity and confusion matrix context.
Handle Edge Cases Deliberately
Evaluation code should define behavior for awkward but real cases:
- one class is absent from
y_true - one class is never predicted
- the denominator for a metric becomes zero
Your metric code should not silently reorder labels either. Always pass an explicit labels array into the confusion matrix so row and column meaning stay stable across runs.
If the label order changes between one script and another, you can generate a perfectly valid confusion matrix with completely wrong human interpretation.
Common Pitfalls
The most common mistake is computing overall accuracy and then calling it enough. That hides poor class-level behavior.
Another common issue is reporting "multiclass sensitivity" or "multiclass specificity" without saying whether the value is macro, weighted, or something else. Developers also often compute specificity incorrectly by forgetting the one-versus-rest formulation and using only row-based counts. Finally, implicit label ordering can produce reports that look correct numerically while labeling the classes wrong.
Summary
- Compute overall accuracy directly from the multiclass confusion matrix.
- Compute sensitivity and specificity one class at a time using a one-versus-rest interpretation.
- Choose an averaging rule such as macro or weighted and report it explicitly.
- Keep label ordering fixed so confusion matrix rows and columns stay meaningful.
- Do not rely on accuracy alone when class imbalance or rare classes matter.
Related reading
- how to calculate PDF in tensorflow
- How to calculate perplexity of RNN in tensorflow
- How to calculate prediction uncertainty using Keras?
- How to Calculate R2 in Tensorflow
- How to calculate optimal batch size?
- How to calculate order big O for more complex algorithms eg quicksort
- How to calculate TFIDF for a single new document to be classified?
- How to calculate the accuracy for multilabel classification with tf.metrics?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the 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.