4×4 Confusion Matrix Calculator and Metrics
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
A 4×4 confusion matrix calculator evaluates a classifier that assigns cases to one of four classes. Enter the 16 actual-versus-predicted counts, and it calculates overall accuracy, Cohen’s kappa, and each class’s precision, recall, and F1 score.
Formula
For a matrix with actual classes in rows and predicted classes in columns, write the counts as nᵢⱼ, where i is the actual class and j is the predicted class. The four diagonal counts are correct predictions. In the calculator, a1 through d4 represent the 16 cells, with the first letter identifying the actual row and the number identifying the predicted column.
- Total N: the sum of all 16 counts.
- Accuracy: (a1 + b2 + c3 + d4) / N × 100%.
- Class 1 precision: a1 / (a1 + b1 + c1 + d1) × 100%. The denominator is everything predicted as class 1.
- Class 1 recall: a1 / (a1 + a2 + a3 + a4) × 100%. The denominator is everything actually in class 1.
- F1 score: 2 × precision × recall / (precision + recall), when those values are defined.
- Cohen’s kappa: (pₒ − pₑ) / (1 − pₑ), where pₒ is observed accuracy and pₑ is the chance agreement expected from the row and column totals.
The displayed percentage outputs use percent units. Kappa is a unitless proportion. This specification shows overall accuracy and kappa plus class 1 precision and recall; calculate the same precision and recall formulas for classes 2–4 by substituting the corresponding diagonal cell and column or row total. Derive each F1 score from that class’s precision and recall.
Worked Example
Using the calculator’s default counts, the row totals are 50, 50, 40, and 35, so N = 175. The column totals are 47, 47, 44, and 37. The diagonal sum is 40 + 35 + 30 + 25 = 130.
- Accuracy: 130 / 175 × 100% = 74.29%.
- Expected agreement: (50×47 + 50×47 + 40×44 + 35×37) / 175² = 7,815 / 30,625 ≈ 0.2552.
- Kappa: (130/175 − 7,815/30,625) / (1 − 7,815/30,625) ≈ 0.6637.
- Class 1 precision: 40 / 47 × 100% = 85.11%.
- Class 1 recall: 40 / 50 × 100% = 80.00%.
The outputs are therefore 74.29% accuracy, kappa 0.6637, 85.11% class 1 precision, and 80.00% class 1 recall. Counts are numbers of cases, not measurements, so they have no metric or US customary unit.
How to Enter and Read a 4×4 Matrix
A confusion matrix compares known (actual) labels with a model’s predicted labels. Choose and keep one orientation: this calculator uses actual classes as rows and predictions as columns. Enter each cell as a nonnegative whole-number count. For example, a1 means actual class 1 predicted as class 1; c4 means actual class 3 predicted as class 4.
Diagonal cells are correct classifications; off-diagonal cells show which classes are being confused. Row totals count actual examples of each class, while column totals count predictions assigned to each class. Match the class order used in your source data before copying values. A matrix transposed relative to the calculator’s convention will swap the interpretation of precision and recall.
Choosing Metrics Beyond Accuracy
Accuracy is easy to interpret as the share of all predictions that are correct, but it can hide poor performance on a less common class. Precision answers, “Of the cases predicted as this class, what share truly belong to it?” Recall answers, “Of the cases actually in this class, what share did the model find?” A high value for one does not guarantee a high value for the other.
F1 combines precision and recall using their harmonic mean. It is useful when both matter, but it does not include true negatives in the same way some other metrics do. For a four-class problem, report class-by-class results as well as any overall summary. Macro averages give each class equal weight; weighted averages weight classes by their number of actual examples. State which averaging method you use, since the values can differ.
Kappa adjusts observed agreement for agreement expected from the class marginals. It is not a replacement for inspecting the matrix, and its interpretation can be affected by class prevalence. Report the underlying counts and relevant per-class metrics so readers can see the pattern rather than relying on one summary score.
Common Problems and Limits
- Wrong orientation: establish whether rows are actual or predicted before interpreting precision and recall.
- Non-count inputs: use counts from the same evaluation set, not percentages mixed with raw counts.
- Zero denominators: precision is undefined if the model never predicts a class; recall is undefined if the evaluation set contains no examples of that class. Do not treat an undefined value as zero without stating that convention.
- Rounding: calculate from unrounded counts and round only displayed results. Rounded class percentages may not reproduce a summary exactly.
- Evaluation leakage: a matrix from training data may overstate performance on new cases. Evaluate on data kept separate from model fitting and tuning.
A confusion matrix summarizes categorical predictions at a chosen decision rule. It does not show why errors happened, whether probabilities are well calibrated, or whether performance will generalize to a different population. The calculator also assumes the entered cases and class definitions are appropriate for the question being assessed.
Frequently Asked Questions
What does a 4×4 confusion matrix represent?
It compares actual and predicted labels for four classes. Each of its 16 cells records how many cases fall into one actual-predicted combination.
How do I calculate accuracy from a 4×4 matrix?
Add the four diagonal counts, then divide by the sum of all 16 cells. Multiply by 100 to express accuracy as a percentage.
Which direction should the rows and columns use?
There is no universal display convention. This calculator places actual classes in rows and predicted classes in columns; check the convention of your data source and transpose values if needed.
Can a confusion matrix have zero values?
Yes. A zero cell means no cases were observed for that combination. But if a class’s entire actual row or predicted column totals zero, its recall or precision, respectively, is undefined.
Is accuracy enough for an imbalanced four-class dataset?
Often not. Review per-class precision and recall, the row and column totals, and the off-diagonal errors. A high overall accuracy can coexist with weak results for a rare class.
Related
Formula used
in a1 | Actual class 1, predicted 1 | count | 40 | 0 in a2 | Actual class 1, predicted 2 | count | 5 | 0 in a3 | Actual class 1, predicted 3 | count | 3 | 0 in a4 | Actual class 1, predicted 4 | count | 2 | 0 in b1 | Actual class 2, predicted 1 | count | 4 | 0 in b2 | Actual class 2, predicted 2 | count | 35 | 0 in b3 | Actual class 2, predicted 3 | count | 6 | 0 in b4 | Actual class 2, predicted 4 | count | 5 | 0 in c1 | Actual class 3, predicted 1 | count | 2 | 0 in c2 | Actual class 3, predicted 2 | count | 3 | 0 in c3 | Actual class 3, predicted 3 | count | 30 | 0 in c4 | Actual class 3, predicted 4 | count | 5 | 0 in d1 | Actual class 4, predicted 1 | count | 1 | 0 in d2 | Actual class 4, predicted 2 | count | 4 | 0 in d3 | Actual class 4, predicted 3 | count | 5 | 0 in d4 | Actual class 4, predicted 4 | count | 25 | 0 out accuracy | Overall accuracy | % | (a1+b2+c3+d4)/(a1+a2+a3+a4+b1+b2+b3+b4+c1+c2+c3+c4+d1+d2+d3+d4)*100 | 2 out kappa | Cohen's kappa | - | ((a1+b2+c3+d4)/(a1+a2+a3+a4+b1+b2+b3+b4+c1+c2+c3+c4+d1+d2+d3+d4)-((a1+a2+a3+a4)*(a1+b1+c1+d1)+(b1+b2+b3+b4)*(a2+b2+c2+d2)+(c1+c2+c3+c4)*(a3+b3+c3+d3)+(d1+d2+d3+d4)*(a4+b4+c4+d4))/(a1+a2+a3+a4+b1+b2+b3+b4+c1+c2+c3+c4+d1+d2+d3+d4)^2)/(1-((a1+a2+a3+a4)*(a1+b1+c1+d1)+(b1+b2+b3+b4)*(a2+b2+c2+d2)+(c1+c2+c3+c4)*(a3+b3+c3+d3)+(d1+d2+d3+d4)*(a4+b4+c4+d4))/(a1+a2+a3+a4+b1+b2+b3+b4+c1+c2+c3+c4+d1+d2+d3+d4)^2) | 4 out precision1 | Class 1 precision | % | a1/(a1+b1+c1+d1)*100 | 2 out recall1 | Class 1 recall | % | a1/(a1+a2+a3+a4)*100 | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.