Weighted Averaging for Imbalanced Classes
What you'll learn: How to calculate fair performance metrics when your dataset has classes of very different sizes.
The Problem with Imbalanced Data
Imagine you're evaluating a model that detects three diseases: one affects 800 patients, another affects 150, and the third affects only 50 patients in your test set. If you simply average metrics across all three classes equally (macro averaging), you're treating the rare disease's performance as equally important as the common one—even though it represents far fewer actual patients.
What is Weighted Averaging?
Weighted averaging adjusts your evaluation metrics by the support of each class—the number of actual samples belonging to that class. Instead of treating all classes equally, you give more influence to classes with more examples.
Think of it like a group project grade: if one team member did 80% of the work, another did 15%, and the third did 5%, a weighted average would reflect those contributions proportionally rather than counting everyone's opinion equally.
The Formula
For a metric like precision, recall, or F1-score:
Weighted Average = Σ(metric_for_class_i × support_i) / total_samples
If Class A has 800 samples with F1=0.90, Class B has 150 samples with F1=0.85, and Class C has 50 samples with F1=0.70:
Weighted F1 = (0.90×800 + 0.85×150 + 0.70×50) / 1000 = 0.882
This gives you a performance metric that reflects how well your model performs on the data as it actually appears in your dataset.
Key Takeaway: Weighted averaging accounts for class imbalance by giving more weight to classes with more samples, providing a realistic overall performance metric that reflects the true data distribution.