scikit-learn
multiclass classification
machine learning
evaluation metrics
python

sklearn metrics for multiclass classification

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

Evaluating multiclass models in scikit-learn requires more than a single accuracy number. Different averaging strategies can tell different stories, especially when classes are imbalanced. A reliable evaluation setup reports per-class metrics, aggregate metrics, and confusion patterns together.

Start with Per-Class Metrics and Confusion Matrix

The fastest way to inspect multiclass performance is classification_report and confusion_matrix.

python
1from sklearn.metrics import classification_report, confusion_matrix
2
3y_true = [0, 1, 2, 2, 1, 0, 2, 1]
4y_pred = [0, 2, 2, 2, 1, 0, 1, 1]
5
6print(classification_report(y_true, y_pred, digits=3))
7print(confusion_matrix(y_true, y_pred))

This gives per-class precision, recall, and F1, plus the actual error distribution by class pair.

Understand Averaging Modes

In multiclass problems, aggregate metrics depend on the averaging scheme.

Common options:

  • 'macro: plain average across classes, treats each class equally'
  • 'weighted: weighted by class support, emphasizes frequent classes'
  • 'micro: global counts of true positives and false positives'
python
1from sklearn.metrics import f1_score, precision_score, recall_score
2
3print("macro f1:", f1_score(y_true, y_pred, average="macro"))
4print("weighted f1:", f1_score(y_true, y_pred, average="weighted"))
5print("micro f1:", f1_score(y_true, y_pred, average="micro"))
6
7print("macro precision:", precision_score(y_true, y_pred, average="macro"))
8print("macro recall:", recall_score(y_true, y_pred, average="macro"))

For imbalanced datasets, macro metrics often expose minority-class weakness that weighted metrics can hide.

Use Probability-Based Metrics When Available

If model exposes class probabilities, add rank-sensitive metrics like log loss.

python
1from sklearn.metrics import log_loss
2import numpy as np
3
4y_true = np.array([0, 1, 2])
5proba = np.array([
6    [0.80, 0.10, 0.10],
7    [0.20, 0.50, 0.30],
8    [0.10, 0.20, 0.70]
9])
10
11print("log loss:", log_loss(y_true, proba))

Probability metrics help evaluate confidence calibration, not just hard-label correctness.

Build a Reusable Evaluation Function

Centralizing evaluation logic keeps experiments comparable.

python
1from sklearn.metrics import classification_report, confusion_matrix, f1_score
2
3
4def evaluate_multiclass(y_true, y_pred):
5    report = classification_report(y_true, y_pred, output_dict=True)
6    cm = confusion_matrix(y_true, y_pred)
7    macro_f1 = f1_score(y_true, y_pred, average="macro")
8    weighted_f1 = f1_score(y_true, y_pred, average="weighted")
9    return {
10        "macro_f1": macro_f1,
11        "weighted_f1": weighted_f1,
12        "confusion_matrix": cm,
13        "report": report,
14    }
15
16result = evaluate_multiclass(y_true=[0, 1, 2, 2], y_pred=[0, 2, 2, 1])
17print(result["macro_f1"])

Use this function in training scripts and CI model checks to avoid metric drift.

Class Imbalance and Threshold Decisions

Multiclass imbalance can make model look good globally while failing critical classes. If some classes are high-risk, define class-specific performance targets and monitor them directly.

Also keep label mappings explicit. Metric errors often come from inconsistent class index mappings between training, inference, and reporting code.

Visualization for Better Error Analysis

A confusion matrix heatmap makes systematic errors easy to spot.

python
1import seaborn as sns
2import matplotlib.pyplot as plt
3from sklearn.metrics import confusion_matrix
4
5cm = confusion_matrix([0, 1, 2, 2], [0, 2, 2, 1])
6sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
7plt.xlabel("Predicted")
8plt.ylabel("True")
9plt.title("Confusion Matrix")
10plt.show()

Visual analysis helps decide whether errors come from class overlap, data quality, or feature gaps.

Cross-Validation Metric Reporting

Single train-test splits can produce unstable multiclass metrics. For reliable model comparisons, compute metrics across folds and report mean plus standard deviation for each key metric. This prevents selecting a model based on one lucky split and makes regression tracking more trustworthy over time.

Common Pitfalls

  • Reporting only accuracy and ignoring class-wise failure patterns.
  • Using weighted average only on heavily imbalanced datasets.
  • Mixing label order between confusion matrix and report outputs.
  • Comparing models with different metric sets across experiments.
  • Ignoring probability calibration when probabilities are used in decisions.

Summary

  • Multiclass evaluation should include per-class and aggregate metrics.
  • Macro, weighted, and micro averages answer different questions.
  • Confusion matrices are essential for error pattern diagnosis.
  • Centralized evaluation utilities improve experiment consistency.
  • Metric choice should align with class risk and business impact.
  • Cross-validation summaries provide more reliable model comparisons than one split.

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.