25. Plotting AI Metrics

Chapter 25 of 36 · 15 min

AI work has specific visualization needs: confusion matrices, ROC curves for classification, precision-recall tradeoffs, embedding projections. Matplotlib handles all of these with the right approach.

Here's a confusion matrix visualization:

import matplotlib.pyplot as plt
import numpy as np

def plot_confusion_matrix(cm, class_names):
    """Plot a confusion matrix with nice formatting."""
    fig, ax = plt.subplots(figsize=(8, 6))
    
    # Use a colormap (lower values = white, higher = dark blue)
    im = ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
    ax.figure.colorbar(im, ax=ax)
    
    # Labels
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           xticklabels=class_names,
           yticklabels=class_names)
    
    # Rotate labels for readability
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
    
    # Annotate cells
    thresh = cm.max() / 2
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], 'd'),
                   ha="center", va="center",
                   color="white" if cm[i, j] > thresh else "black")
    
    ax.set_xlabel('Predicted Label')
    ax.set_ylabel('True Label')
    ax.set_title('Confusion Matrix')
    
    plt.tight_layout()
    return fig

# Simulate confusion matrix (rows=true, cols=predicted)
# Classes: ['Negative', 'Neutral', 'Positive']
cm = np.array([
    [142, 8, 5],
    [12, 118, 10],
    [3, 9, 143]
])

fig = plot_confusion_matrix(cm, ['Negative', 'Neutral', 'Positive'])
plt.show()

For ROC curves and other sklearn metrics, the sklearn.metrics module has direct plotting support:

from sklearn.metrics import roc_curve, auc, RocCurveDisplay

# Simulated predictions and ground truth
y_true = [0, 0, 0, 1, 1, 1, 1]
y_scores = [0.1, 0.35, 0.4, 0.6, 0.65, 0.8, 0.95]

fpr, tpr, thresholds = roc_curve(y_true, y_scores)
roc_auc = auc(fpr, tpr)

fig, ax = plt.subplots()
RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc).plot(ax=ax)
plt.show()

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Create a figure with two subplots side by side: one showing an ROC curve for a good classifier (high AUC ~0.95) and one for a near-random classifier (AUC ~0.55). Use simulated data. Add AUC values as text annotations.