6.3 Visualizations for ML
Introduction
Visualization is essential for understanding model behavior, debugging performance issues, and communicating results. For LLMs, visualizations help interpret attention patterns, token distributions, and embedding spaces.
1. Decision Boundaries
Decision boundaries show how a classifier divides the feature space into class regions. For a binary classifier \( f: \mathbb{R}^2 \rightarrow \{0, 1\} \), the decision boundary is: $$\{x \in \mathbb{R}^2 : P(y=1|x) = 0.5\}$$
Interactive: Decision Boundary Visualization
Compare decision boundaries of different classifiers:
Interpretation: Colors show predicted class regions
Accuracy: {{accuracy | number:1}}%
2. Confusion Matrix
The confusion matrix shows the performance of a classification model:
| Predicted | |||
| Positive | Negative | ||
| Actual | Positive | TP (True Positive) | FN (False Negative) |
| Negative | FP (False Positive) | TN (True Negative) | |
Interactive: Confusion Matrix
Adjust the classification threshold and see how the confusion matrix changes:
Accuracy: {{confusionMetrics.accuracy | number:3}}
Precision: {{confusionMetrics.precision | number:3}}
Recall: {{confusionMetrics.recall | number:3}}
F1-Score: {{confusionMetrics.f1 | number:3}}
3. ROC Curve
The Receiver Operating Characteristic (ROC) curve plots True Positive Rate vs False Positive Rate: $$\text{TPR} = \frac{TP}{TP + FN}, \quad \text{FPR} = \frac{FP}{FP + TN}$$ The Area Under the Curve (AUC) measures overall performance: AUC = 1 is perfect, AUC = 0.5 is random.
Interactive: ROC Curve
See how model quality affects the ROC curve:
AUC: {{aucValue | number:3}}
Interpretation: {{aucInterpretation}}
4. Learning Curves
Learning curves show how model performance changes with training data size. They help diagnose:
- Underfitting: Both training and validation error are high
- Overfitting: Large gap between training and validation error
- Good Fit: Both errors are low and converge
Interactive: Learning Curves
Simulate different learning scenarios:
Diagnosis: {{learningDiagnosis}}
Recommendation: {{learningRecommendation}}
5. Attention Heatmap (for LLMs)
Attention mechanisms in transformers show which tokens the model focuses on. For query \( Q \), key \( K \), and value \( V \): $$\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V$$ The attention weights \( \text{softmax}(QK^T/\sqrt{d_k}) \) can be visualized as a heatmap.
Interactive: Attention Pattern Visualization
Visualize how tokens attend to each other:
Pattern: {{attentionDescription}}
6. Feature Importance
Feature importance shows which features contribute most to predictions. For tree-based models: $$\text{Importance}(f) = \sum_{t \in \text{splits on } f} \Delta \text{Impurity}_t \cdot n_t$$ where \( \Delta \text{Impurity}_t \) is the impurity reduction and \( n_t \) is the number of samples at node \( t \).
Interactive: Feature Importance
Compare feature importances across different models:
Top 3 Features: {{topFeatures}}
Explanation: Radar view normalizes each model's importances to its max for multi-model comparison.
Visualization Best Practices
- Choose the Right Plot: Match visualization to the question you're answering
- Use Color Wisely: Sequential for continuous, diverging for positive/negative, categorical for classes
- Label Everything: Axes, legends, titles make plots self-explanatory
- Show Uncertainty: Include confidence intervals, error bars when relevant
- Interactive is Better: Allow users to explore data dynamically
- Keep it Simple: Don't overcomplicate; clarity over complexity
Common Visualization Types
| Visualization | Use Case | Key Insight |
|---|---|---|
| Scatter Plot | Relationships between variables | Correlation, clusters, outliers |
| Histogram | Distribution of single variable | Shape, center, spread, skewness |
| Box Plot | Compare distributions across groups | Median, quartiles, outliers |
| Heatmap | Matrix data, correlations | Patterns, clusters, relationships |
| Line Plot | Trends over time/sequence | Temporal patterns, convergence |
| Bar Chart | Compare quantities across categories | Rankings, comparisons |