
Macro, Micro or Weighted? How Averaging Hides Your Worst Class
- كاتب موثّق
- scikit-learn 1.9
محتويات المقال
The common misconception
F1 is the metric you switch to when accuracy misleads, so it feels like the fair one by default. scikit-learn's f1_scorerefuses multiclass targets until you choose anaverage, 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.
What actually happens
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.
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.918from sklearn.datasets import make_classificationfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import f1_score, recall_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) macro = f1_score(y_te, pred, average="macro") print(f"class_weight={cw}: macro F1 {macro:.3f}") print(" recall per queue", recall_score(y_te, pred, average=None).round(2))# class_weight=None: macro F1 0.815# recall per queue [0.98 0.95 0.82 0.82 0.39]# class_weight=balanced: macro F1 0.832# recall per queue [0.95 0.93 0.75 0.83 0.92]Macro F1 is a policy too
micro | |||
weighted | |||
macro | |||
None |
How to apply it in your project
Print the per-class report Run classification_reporton every evaluation: precision, recall, F1 and support for each class, plus accuracy and the macro and weighted averages.Choose the average from the question If 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. Set floors for critical classes Give classes like security a minimum recall, and fail any model below it, whatever its average. Fix the class list Pass labels=explicitly, stratify your folds, and setzero_divisiondeliberately, so averages compare across folds and models.Look where the errors go A 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.
التعليقات (0)