multi-label classification
confusion matrix
machine learning
classifier evaluation
data science

Understanding multi-label classifier using 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.

Practice ML system design

Introduction to Multi-label Classification

Multi-label classification is an extension of traditional classification problems where each instance can be associated with multiple classes rather than a single label. This scenario is common in various applications, such as tagging images with multiple attributes, categorizing emails by multiple topics, or labeling music tracks with multiple genre categories.

The Role of Confusion Matrices

A confusion matrix is a crucial tool in evaluating the performance of classification models. In binary and multi-class classification problems, it helps one observe the number of true positives (TP), false positives (FP), true negatives (TN), and false negatives (FN). However, multi-label classification introduces additional complexity due to its inherent aspect of allowing multiple labels per instance.

Multi-label Confusion Matrix

For multi-label classification, a typical one-versus-all confusion matrix might not be sufficient since it’s not tailored for multiple true labels per sample. Instead, a more generalized confusion matrix can be constructed that corresponds to how well the model predicts all the labels for each instance. Let's delve into its construction and utility.

Per-Label Confusion Matrix

In multi-label classification, you often evaluate each label independently in a one-versus-all fashion to build confusion matrices for individual labels. This means you calculate individual matrices for each label across all samples, which allows calculating various performance metrics like precision, recall, and F1-score for each label. The following table describes how these can be structured:

MetricDescription
True Positives (TP)Instances where the label was both predicted and ground truth.
False Positives (FP)Instances where the label was predicted but not actually present.
False Negatives (FN)Instances where the label was not predicted but was present in the ground truth.
True Negatives (TN)Instances where the label was neither predicted nor present.

Given this breakdown, metrics can be calculated as follows:

Precision: TPTP+FP\frac{TP}{TP + FP}Recall: TPTP+FN\frac{TP}{TP + FN}F1-Score: 2×Precision×RecallPrecision+Recall2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}

Aggregated Metrics

While label-wise metrics provide insight at a granular level, aggregated metrics can offer a broader validation of the model’s performance. Commonly used aggregated metrics in multi-label settings include:

Micro-averaging: Aggregates contributions from all labels to compute metrics globally. • Macro-averaging: Computes metrics for each label and then averages them. • Subset accuracy: Measures the proportion of completely correctly predicted label sets.

Worked Example

To understand these concepts better, let's work through a practical example. Consider a dataset with three labels: A, B, and C.

SampleTrue LabelsPredicted Labels
1A, BA, C
2AA
3B, CB
4CA, C

Let's break down Sample 1: For label A, it's a TP; for B, a FN; and for C, a FP. Similar analyses can be done for other samples.

Sample Data Evaluation

Let's summarize this example in a confusion matrix:

MetricABC
TP211
FP001
FN011
TN222

Metrics Calculation

Using these matrices:

Precision for A: 22=1.0\frac{2}{2} = 1.0Recall for B: 12=0.5\frac{1}{2} = 0.5F1-Score for C: 2×11+1×11+111+1+11+1=0.672 \times \frac{\frac{1}{1+1} \times \frac{1}{1+1}}{\frac{1}{1+1} + \frac{1}{1+1}} = 0.67

This example illustrates the detailed analysis possible with a confusion matrix across multiple labels.

Conclusion

Understanding and using a confusion matrix for multi-label classifiers enable comprehensive performance evaluation at both the individual label and global level. This multidimensional analysis helps improve model robustness in real-world applications where multi-label classification prevails. The challenge lies in balancing precision and recall across the diverse labels, hence metrics aggregation can be pivotal in guiding model improvements. As multi-label data becomes more ubiquitous, proficiency with these evaluation strategies will be increasingly important for data scientists and developers.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.