Inspect per-class performance
Model evaluation
Scikit-Learn · Function
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.
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.
sklearn.metrics.classification_report(y_true, y_pred, *, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')| Name | Type | Description |
|---|---|---|
y_true | 1d array-like, or label indicator array / sparse matrix | Ground-truth target values; supports one-dimensional labels or label-indicator data. |
y_pred | 1d array-like, or label indicator array / sparse matrix | Estimated targets produced by a classifier. |
labels | array-like, default=None | Labels to include in the report. |
target_names | list of str, default=None | Display names corresponding to labels in the same order. |
sample_weight | array-like of shape (n_samples,), default=None | Per-sample weights. |
digits | int, default=2 | Decimal precision for formatted output. |
output_dict | bool, default=False | Return a dictionary instead of formatted text. |
zero_division | warn, 0.0, 1.0, or np.nan, default='warn' | Controls handling of zero-division cases. |
str or dict: a text summary, or a dictionary when output_dict=True.
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.
Model evaluation
Imbalanced classification
Evaluation pipelines
Multilabel classification
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.
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.
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_supportmultilabel_confusion_matrixaccuracy_scoreConfusionMatrixDisplay