Weighted Averaging

Weighted averaging computes a metric per class and averages it in proportion to each class’s support โ€” the number of true instances it has:

\[ M_{weighted} = \frac{\sum_{i=1}^{C} n_i \, M_i}{\sum_{i=1}^{C} n_i} \]

It is presented as the middle ground: macro gives every class one vote, micro gives every sample one vote, and weighted gives every class a vote proportional to its size. Two things about it are not obvious from the formula.

The F1 need not lie between the precision and the recall

import numpy as np
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score

support = np.array([1000, 100, 10])
per_class_f1 = np.array([0.95, 0.60, 0.20])
print(f"supports {support.tolist()}, per-class F1 {per_class_f1.tolist()}")
print(f"  macro F1    {per_class_f1.mean():.4f}")
print(f"  weighted F1 {np.sum(support * per_class_f1) / support.sum():.4f}"
      f"   = {np.sum(support * per_class_f1):.0f} / {support.sum()}")

y = np.repeat([0, 1, 2], [600, 300, 100])
pred = y.copy()
pred[np.flatnonzero(y == 0)[:540]] = 1
"""Class 0 is mostly predicted as class 1. Class 1 now has near-perfect recall and
   poor precision; class 0 has the reverse. Each per-class F1 is punished for that
   imbalance, and the weighted average inherits the punishment."""

wp = precision_score(y, pred, average="weighted", zero_division=0)
wr = recall_score(y, pred, average="weighted", zero_division=0)
wf = f1_score(y, pred, average="weighted", zero_division=0)
print(f"\n  weighted precision {wp:.4f}   weighted recall {wr:.4f}   weighted F1 {wf:.4f}")
print(f"  is the F1 between the other two?  {min(wp, wr) <= wf <= max(wp, wr)}")
print(f"  weighted recall equals accuracy:  {wr:.4f} and {accuracy_score(y, pred):.4f}")
print("  per-class precision " + "  ".join(f"{v:.4f}" for v in precision_score(y, pred, average=None, zero_division=0)))
print("  per-class recall    " + "  ".join(f"{v:.4f}" for v in recall_score(y, pred, average=None, zero_division=0)))
# supports [1000, 100, 10], per-class F1 [0.95, 0.6, 0.2]
#   macro F1    0.5833
#   weighted F1 0.9117   = 1012 / 1110
#
#   weighted precision 0.8071   weighted recall 0.4600   weighted F1 0.3670
#   is the F1 between the other two?  False
#   weighted recall equals accuracy:  0.4600 and 0.4600
#   per-class precision 1.0000  0.3571  1.0000
#   per-class recall    0.1000  1.0000  1.0000

The first block is the standard worked example, with the arithmetic done: supports of 1000, 100 and 10 against per-class F1 scores of 0.95, 0.60 and 0.20 give \(1012/1110 = 0.9117\), against a macro F1 of 0.5833. The weighted figure sits close to the largest class, which is the point of it.

The second block is the one to remember. Weighted precision 0.8071, weighted recall 0.4600, weighted F1 0.3670 โ€” below both. scikit-learn’s documentation states this directly: weighted averaging “can result in an F-score that is not between precision and recall.”

The mechanism is in the per-class rows. Class 0 has precision 1.0000 and recall 0.1000; class 1 has precision 0.3571 and recall 1.0000. Averaging precision across classes and averaging recall across classes each produce a respectable number, because every class is excellent at one of the two. But F1 is computed per class first, and a harmonic mean punishes imbalance severely โ€” a class with 1.00 and 0.10 has an F1 of 0.18, not 0.55. Averaging those already-punished values gives something below both averages.

So a reader who sees weighted precision and recall and expects the F1 to fall between them will conclude something has gone wrong. Nothing has. The three numbers answer three questions and are not constrained to be consistent with each other.

The weighted recall is accuracy

The last line: weighted recall 0.4600 and accuracy 0.4600. That is an identity, not a coincidence. Class \(i\)’s recall is its correctly classified count over \(n_i\); weighting by \(n_i\) and dividing by the total cancels the denominators and leaves total correct over total samples.

Weighted recall equals micro recall in this complete single-label setting. Weighted precision and weighted F1 are different aggregations, but neither is guaranteed to sit between its macro and micro counterparts. For example, the same predictions can yield macro F1 0.3106, micro F1 0.3333 and weighted F1 0.3685.

When to use it

Weighted averaging is right when the classes’ importance really does scale with how often they occur โ€” a support-ticket router where every ticket costs the same to misroute, a recommendation system where volume is the business. In those cases it answers the question directly.

It is the wrong default on imbalanced data where the rare class is the point. Weighting by support means the rare class contributes almost nothing, so a model that abandons it entirely still posts a healthy weighted score โ€” which is the failure macro averaging exists to expose.

Report the averaging scheme, per-class scores and supports together. Macro and weighted summaries can emphasize different parts of the class distribution; they do not provide bounds on an underlying answer.

References


Discover more from Insightful Data Lab

Subscribe to get the latest posts sent to your email.

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.