How to normalize a confusion matrix?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Understanding Confusion Matrices
Before delving into the normalization of a confusion matrix, it's essential to recap what a confusion matrix is. A confusion matrix is a tool used in machine learning to evaluate the performance of a classification algorithm. It is a table with two dimensions: actual classifications and predicted classifications. It juxtaposes these to reveal how well a model is performing.
Here’s a typical structure of a confusion matrix for a binary classification problem:
| Actual \ Predicted | Positive | Negative |
| Positive | TP | FN |
| Negative | FP | TN |
Where:
- TP (True Positive): The number of correct positive predictions.
- TN (True Negative): The number of correct negative predictions.
- FP (False Positive): The number of incorrect positive predictions.
- FN (False Negative): The number of incorrect negative predictions.
Importance of Normalizing a Confusion Matrix
Normalization of a confusion matrix helps to represent the proportions of true versus false predictions, making it independent of the absolute number of predictions. This is particularly useful when dealing with imbalanced datasets or when models are being compared that have different prediction totals.
Methods for Normalization
Normalization can be conducted in multiple ways, but the general principle is to convert the count matrix into a probability matrix. Below are the typical methods employed:
1. Row-wise Normalization
Row-wise normalization involves dividing each entry in a row by the sum of that row. This method presents the probability of each predicted class concerning the true class.
Formula:
For a row with entries a_(ij):
Example:
Given a confusion matrix:
| Actual \ Predicted | Positive | Negative |
| Positive | 50 | 10 |
| Negative | 5 | 35 |
Row-wise normalization:
| Actual \ Predicted | Positive | Negative |
| Positive | 0.83 | 0.17 |
| Negative | 0.125 | 0.875 |
2. Column-wise Normalization
Column-wise normalization involves dividing each entry in a column by the sum of that column. This approach is less common but useful when the focus is on how predicted classifications distribute across actual classes.
Formula:
For a column with entries a_(ij):
Example:
Column-wise normalization of the previous matrix:
| Actual \ Predicted | Positive | Negative |
| Positive | 0.91 | 0.22 |
| Negative | 0.09 | 0.78 |
3. Full Normalization
Full normalization involves dividing each entry by the total number of predictions, converting to a full probability distribution.
Formula:
Example:
Fully normalizing the original matrix (Total = 100):
| Actual \ Predicted | Positive | Negative |
| Positive | 0.5 | 0.1 |
| Negative | 0.05 | 0.35 |
When to Use Each Method
- Row-wise Normalization: Use this method to understand the model's performance for each actual class separately, especially with class imbalance.
- Column-wise Normalization: This is useful for evaluating prediction distribution and ensuring fairness across predicted classes.
- Full Normalization: Ideal for conveying the confusion matrix's insights as overall probabilities, often used when comparing models with varying test sizes.
Summary Table: Key Points of Methods
| Method | Focus | Formula | Use Cases |
| Row-wise | True Class Distribution | frac(a_(ij))(∑_(j) a_(ij)) | Handling imbalanced classes Sensitivity analysis |
| Column-wise | Predicted Class Dist. | frac(a_(ij))(∑_(i) a_(ij)) | Prediction fairness Precision analysis |
| Full | Overall Probability | frac(a_(ij))(∑_(i)∑_(j) a_(ij)) | Comparing different-sized datasets |
Conclusion
Normalization of a confusion matrix is a powerful technique to interpret classification results more comprehensively. It balances the representation of performance metrics irrespective of the scale of datasets used. By choosing the right normalization technique, practitioners can gain deeper insights into the performance nuances of their classification models.
Related reading
- How to normalize a NumPy array to within a certain range?
- how to normalize input data for models in tensorflow
- How to normalize the Train and Test data using MinMaxScaler sklearn
- How to obtain features' weights
- How to normalize a numpy array to a unit vector
- How to pass another entire column as argument to pandas fillna
- How to optimize for inference a simple, saved TensorFlow 1.0.1 graph?
- How to optimize for multiple metrics in Optuna

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.