confusion matrix
normalization
machine learning
data science
performance metrics

How to normalize a confusion matrix?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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 \ PredictedPositiveNegative
PositiveTPFN
NegativeFPTN

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):

 
(Row-wise Normalized ) a_(ij) = frac(a_(ij))(∑_(j) a_(ij))

Example:

Given a confusion matrix:

Actual \ PredictedPositiveNegative
Positive5010
Negative535

Row-wise normalization:

Actual \ PredictedPositiveNegative
Positive0.830.17
Negative0.1250.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):

 
(Column-wise Normalized ) a_(ij) = frac(a_(ij))(∑_(i) a_(ij))

Example:

Column-wise normalization of the previous matrix:

Actual \ PredictedPositiveNegative
Positive0.910.22
Negative0.090.78

3. Full Normalization

Full normalization involves dividing each entry by the total number of predictions, converting to a full probability distribution.

Formula:

 
(Fully Normalized ) a_(ij) = frac(a_(ij))(∑_(i)∑_(j) a_(ij))

Example:

Fully normalizing the original matrix (Total = 100):

Actual \ PredictedPositiveNegative
Positive0.50.1
Negative0.050.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

MethodFocusFormulaUse Cases
Row-wiseTrue Class Distributionfrac(a_(ij))(∑_(j) a_(ij))Handling imbalanced classes Sensitivity analysis
Column-wisePredicted Class Dist.frac(a_(ij))(∑_(i) a_(ij))Prediction fairness Precision analysis
FullOverall Probabilityfrac(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.


Course illustration
Course illustration

All Rights Reserved.