Class-level metrics show where a classifier performs well and where it misses specific labels. In scikit-learn, classification_report() turns held-out labels and predictions into precision, recall, F1-score, and support rows that are easier to inspect than a single accuracy value.
The report takes the true labels and predicted labels from the same evaluation split. Passing target_names keeps rows readable when labels are encoded as integers, and zero_division=0 makes unattended checks deterministic if a class has no predicted samples.
Use the text output for notebook review, pull output_dict=True when a pipeline needs numeric metric values, and read the aggregate rows alongside per-class support. A high weighted average can hide a weak minority-class row, so the support counts matter whenever classes are imbalanced.
from sklearn.datasets import load_wine from sklearn.metrics import classification_report from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier wine = load_wine() X_train, X_test, y_train, y_test = train_test_split( wine.data, wine.target, test_size=0.30, stratify=wine.target, random_state=7, ) model = DecisionTreeClassifier(max_depth=3, random_state=7) model.fit(X_train, y_train) y_pred = model.predict(X_test) print( classification_report( y_test, y_pred, target_names=wine.target_names, digits=3, zero_division=0, ) ) metrics = classification_report( y_test, y_pred, target_names=wine.target_names, output_dict=True, zero_division=0, ) print(f"macro avg f1-score: {metrics['macro avg']['f1-score']:.3f}")
Replace the load_wine() split with the project's existing test labels and predictions when the classifier is already trained.
Related: How to split data into train and test sets with scikit-learn
$ python3 classification_report_demo.py
precision recall f1-score support
class_0 1.000 0.833 0.909 18
class_1 0.870 0.952 0.909 21
class_2 0.938 1.000 0.968 15
accuracy 0.926 54
macro avg 0.936 0.929 0.929 54
weighted avg 0.932 0.926 0.925 54
macro avg f1-score: 0.929
metrics = classification_report( y_test, y_pred, target_names=wine.target_names, output_dict=True, zero_division=0, ) print(f"macro avg f1-score: {metrics['macro avg']['f1-score']:.3f}")
digits affects only the formatted text report. The dictionary returned by output_dict=True keeps unrounded numeric values.
The support values show how many held-out samples belong to each class. In the sample output, 18 + 21 + 15 matches the 54 samples shown on the accuracy and aggregate rows.