الانتقال إلى المحتوى
Machine Learningتقييم الموديلات· 7 دقائق للقراءة

Macro, Micro or Weighted? How Averaging Hides Your Worst Class

Each average decides how much every class counts. Micro F1 is accuracy, weighted F1 follows the biggest class, and only per-class scores show the one that's failing.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • scikit-learn 1.9
محتويات المقال

A support team routes incoming tickets with a classifier: billing, technical, account, refunds and security. The model card says weighted F1 0.924. A candidate trained with class weights scores 0.918, so the team keeps the original. Two weeks later the security lead asks why account-takeover reports sit in the account queue for days. On the test set the original model sent 15 of the 38 security tickets there and routed only 15 correctly. The rejected candidate routed 35. No number on the model card could have shown it.

The common misconception

Many developers read a multiclass F1 score as a summary of every class: if the overall number is high, each class must be in decent shape. It's an easy belief to hold:

  • F1 is the metric you switch to when accuracy misleads, so it feels like the fair one by default.
  • scikit-learn's f1_score refuses multiclass targets until you choose an average, so whichever one you pick looks like the official answer.
  • Weighted averaging sounds like the careful choice: the documentation says it accounts for label imbalance.

The half-truth: every average is built from per-class scores, so each class is in there somewhere. But each average also decides how much each class counts, and for a rare class the answer can be "almost nothing".

What actually happens

For a multiclass problem, scikit-learn computes precision, recall and once per class, treating that class as positive and all the others as negative. Then the average parameter collapses those per-class scores into one number. The three averages answer three different questions.

Micro F1 is accuracy under another name

average="micro" pools the true positives, false positives and false negatives of all classes before dividing. In single-label multiclass, every wrong prediction is a false positive for the class it chose and a false negative for the class it missed, so the totals match. The scikit-learn guide states the result: with all labels included, micro precision, recall and F1 are all identical to accuracy. classification_report doesn't even print a micro average in that case, because it would repeat accuracy. Micro F1 is a fine metric; it just isn't a different one from the accuracy you were trying to get away from.

Weighted F1 follows the biggest class

average="weighted" averages the per-class scores weighted by support, the number of true examples of each class. In the test set below, billing has 683 of 1,250 tickets and security has 38, so billing carries a weight of 0.55 and security 0.03. The documentation adds a quirk: a weighted F1 can even fall outside the range between the weighted precision and recall.

Macro F1 gives every class one vote

average="macro" is the plain mean of the per-class scores. A rare class counts as much as the biggest one, which is why the scikit-learn guide suggests when infrequent classes still matter. Here are both candidates, judged first by the two averages that count tickets, then class by class.

Python
from sklearn.datasets import make_classificationfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import f1_scorefrom sklearn.model_selection import train_test_split# five ticket queues: billing, technical, account, refunds, securityX, y = make_classification(n_samples=5000, n_features=10, n_informative=6,                           n_classes=5, n_clusters_per_class=1, class_sep=1.2,                           weights=[0.55, 0.25, 0.1, 0.07], random_state=3)X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)for cw in [None, "balanced"]:    pred = LogisticRegression(class_weight=cw).fit(X_tr, y_tr).predict(X_te)    micro = f1_score(y_te, pred, average="micro")    weighted = f1_score(y_te, pred, average="weighted")    print(f"class_weight={cw}: micro {micro:.3f}, weighted {weighted:.3f}")# class_weight=None: micro 0.927, weighted 0.924# class_weight=balanced: micro 0.914, weighted 0.918

By micro and weighted F1 the two models are nearly tied, and both numbers prefer the original: 0.927 against 0.914, 0.924 against 0.918. The micro scores are exactly the two models' accuracies. Class by class, the story changes: the original finds 39% of security tickets, the class-weighted model 92%, while billing recall slips from 0.98 to 0.95. Macro F1 sees that trade and prefers the candidate, 0.832 against 0.815.

Weighted F1 moved the other way for a simple reason. Billing's F1 fell by 0.013 with a weight of 0.55, which costs 0.007. Security's F1 rose from 0.500 to 0.625 with a weight of 0.03, which adds 0.004. With a small loss on account tickets too, the total drops by 0.006. Weighted F1 did what it's built to do: count tickets, not queues.

Macro F1 is a policy too

Macro isn't automatically right. The scikit-learn guide warns that treating all classes as equally important is often untrue, and macro averaging then over-emphasizes the typically low score of a rare class. Rare classes are also noisy: with 38 security tickets, one ticket moves security recall by 2.6 points. And "macro F1" has two definitions in the literature: scikit-learn averages the per-class F1 scores, while others take the F1 of the averaged precision and recall. Opitz and Burst show the two can rank classifiers differently; for the original model here they give 0.815 and 0.822. When you compare with a paper, check which one it used.

AverageHow it's computedWho dominatesThe question it answers
microPools TP, FP and FN across classesThe largest classes; equals accuracyHow often is one prediction right?
weightedMean of per-class scores, weighted by supportThe largest classesHow good is it per example, in per-class terms?
macroPlain mean of per-class scoresEvery class equally, rare ones includedHow good is it on a typical class?
NoneOne score per classNothing is hiddenWhich class is failing?

How to apply it in your project

  1. Print the per-class reportRun classification_report on every evaluation: precision, recall, F1 and support for each class, plus accuracy and the macro and weighted averages.
  2. Choose the average from the questionIf every example matters equally, accuracy or weighted F1 fit. If every class matters, use macro. Write the choice and the reason in the model card.
  3. Set floors for critical classesGive classes like security a minimum recall, and fail any model below it, whatever its average.
  4. Fix the class listPass labels= explicitly, stratify your folds, and set zero_division deliberately, so averages compare across folds and models.
  5. Look where the errors goA shows that the missing security tickets landed in the account queue, which tells you what to fix next.

Key takeaways

  • Every average is a weighting policy: micro and weighted count examples, macro counts classes.
  • In single-label multiclass, micro F1 is exactly accuracy.
  • Weighted F1 preferred the model that missed 23 of 38 security tickets; per-class recall showed 0.39 against 0.92.
  • Report per-class scores with support, choose the average on purpose, and guard critical classes with a floor.

📚 المصادر

  1. Metrics and scoring: quantifying the quality of predictions — scikit-learn User Guide
  2. f1_score — scikit-learn API Reference
  3. classification_report — scikit-learn API Reference
  4. Opitz and Burst, Macro F1 and Macro F1 (arXiv 2019)

التعليقات (0)