Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

classification_report

Builds a per-class classification-metrics report from true and predicted labels. It returns formatted text by default or structured data when output_dict=True.

How it works

classification_report compares ground-truth labels with classifier predictions and summarizes precision, recall, F1 score, and support for every included class. It can format the result as text or return a dictionary for programmatic inspection.

Syntax

sklearn.metrics.classification_report(y_true, y_pred, *, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')

Parameters

NameTypeDescription
y_true1d array-like, or label indicator array / sparse matrixGround-truth target values; supports one-dimensional labels or label-indicator data.
y_pred1d array-like, or label indicator array / sparse matrixEstimated targets produced by a classifier.
labelsarray-like, default=NoneLabels to include in the report.
target_nameslist of str, default=NoneDisplay names corresponding to labels in the same order.
sample_weightarray-like of shape (n_samples,), default=NonePer-sample weights.
digitsint, default=2Decimal precision for formatted output.
output_dictbool, default=FalseReturn a dictionary instead of formatted text.
zero_divisionwarn, 0.0, 1.0, or np.nan, default='warn'Controls handling of zero-division cases.

Returns

str or dict: a text summary, or a dictionary when output_dict=True.

Key points

  • The default result is a human-readable report of the principal classification metrics.
  • Each class receives precision, recall, F1 score, and support information.
  • Use labels to restrict which class labels appear in the report.
  • Use target_names to replace raw labels with display names, preserving label order.
  • digits changes the number of decimal places used when formatting numeric text output.
  • The report includes a macro average alongside other applicable aggregate averages.

Common mistakes

  • Sparse target matrices are supported only for multilabel targets.
  • Set output_dict=True when downstream code needs structured values rather than formatted text.
  • The sample average is available only for multilabel classification.

Example: Print a classification report

from sklearn.metrics import classification_report

y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]

print(classification_report(y_true, y_pred, target_names=["class 0", "class 1", "class 2"]))

Prints a readable per-class precision, recall, F1-score, and support report.

When to use it

Inspect per-class performance

Model evaluation

Compare macro and weighted averages

Imbalanced classification

Consume structured metric data

Evaluation pipelines

Review sample-level averages

Multilabel classification

Interview questions

QWhat does classification_report compute?

It accepts true labels and predicted labels, then reports precision, recall, F1 score, and support for each class. By default it returns a formatted text summary.

Follow-up: How can code consume the metrics without parsing text? Set output_dict=True, which returns a dictionary.

QHow do the reported averages differ?

The macro average is the unweighted mean across labels, while the weighted average weights each label by its support. A sample average applies only to multilabel classification.

Follow-up: When can a micro average appear? It is shown for multilabel data or multiclass data restricted to a class subset, where it is not equivalent to ordinary accuracy.

QHow do labels, target_names, and digits affect the output?

labels selects the label indices included in the report, and target_names supplies display names in the same order. digits controls formatted floating-point precision for text output.

Follow-up: What should be checked when custom names are supplied? Ensure target_names corresponds positionally to the selected labels.

  • precision_recall_fscore_support
  • multilabel_confusion_matrix
  • accuracy_score
  • ConfusionMatrixDisplay

Source: classification_report — scikit-learn 1.9.1 documentation. Checked 2026-10-15.