Week 12 โ Visualization for ML/NLP
Visualizing machine learning and natural language processing outputs for interpretability and insight.
๐ Background & Motivation
Modern machine learning and NLP models generate complex outputs โ from feature importances and prediction probabilities to topic clusters and embeddings. Visualization provides the bridge between model mechanics and human understanding. This week focuses on how to visualize model performance, interpret model decisions, and explore textual data patterns.
๐ Learning Objectives
- Plot feature importance for classification and regression models.
- Visualize model performance with confusion matrices and ROC curves.
- Use embeddings and clustering visualizations to interpret NLP models.
- Generate word clouds and topic cluster plots using BERTopic.
- Understand visualization's role in the ML workflow for interpretability and trust.
๐ Readings & Resources
- Hands-On Machine Learning with Scikit-Learn & TensorFlow โ chapters on model evaluation.
- scikit-learn: Model Evaluation & Visualization
- BERTopic: Topic Modeling & Visualization
- WordCloud: Python WordCloud Library
- Interpretable ML Book by Christoph Molnar
Sample Datasets:
- IMDB Reviews (text sentiment classification)
- News Categories or 20 Newsgroups dataset
- Any scikit-learn classification dataset (e.g., iris, digits)
๐ ๏ธ Setup Checklist
Ensure your environment includes:
pip install scikit-learn matplotlib seaborn wordcloud bertopic numpy pandas
pip install jupyter_compare_view datasets umap-learn hdbscan sentence-transformers
Confirm you can train a simple classifier, generate predictions, and render evaluation plots.
๐งญ Lecture Outline
Session 1 (75 min โ Theory Focus)
- Role of visualization in the ML workflow (10 min)
- Model interpretability and explainability (10 min)
- Feature importance and coefficient visualization (20 min)
- Model evaluation plots: confusion matrix, ROC curve, precision-recall (20 min)
- Discussion: visualization ethics and bias (15 min)
Session 2 (75 min โ Hands-on Focus)
- Train and evaluate a classifier (IMDB reviews or scikit-learn dataset) (20 min)
- Plot confusion matrix and ROC curve (20 min)
- Visualize feature importances or SHAP-like explanations (15 min)
- Generate word clouds and BERTopic clusters for NLP data (15 min)
- Mini-workshop: interpret model predictions visually (5 min)
- Download the demo files for ML classification here
- Download the demo files NLP Topic Modeling here
๐ป Starter Notebook Snippets
Feature Importance Plot
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
import pandas as pd
import matplotlib.pyplot as plt
X, y = load_iris(return_X_y=True, as_frame=True)
model = RandomForestClassifier(random_state=42).fit(X, y)
importances = pd.Series(model.feature_importances_, index=X.columns).sort_values()
importances.plot(kind='barh', color='#4e79a7')
plt.title('Feature Importance โ Random Forest')
plt.xlabel('Importance Score')
plt.show()
Confusion Matrix and ROC Curve
from sklearn.metrics import ConfusionMatrixDisplay, RocCurveDisplay, classification_report
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
ConfusionMatrixDisplay.from_estimator(model, X_test, y_test)
plt.title('Confusion Matrix')
plt.show()
RocCurveDisplay.from_estimator(model, X_test, y_test)
plt.title('ROC Curve')
plt.show()
Word Cloud and Topic Clusters
from wordcloud import WordCloud
import matplotlib.pyplot as plt
text = ' '.join(['great movie', 'terrible film', 'excellent acting', 'boring story'])
wc = WordCloud(width=600, height=300, background_color='white').generate(text)
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.title('Example Word Cloud')
plt.show()
from bertopic import BERTopic
from sklearn.datasets import fetch_20newsgroups
newsgroups = fetch_20newsgroups(subset='all')
docs = newsgroups.data[:200]
model = BERTopic(verbose=True)
topics, probs = model.fit_transform(docs)
model.visualize_topics()
๐งช In-Class Activity
- Plot feature importance and discuss which features drive predictions.
- Evaluate model performance visually using confusion matrix and ROC curve.
- Generate a word cloud summarizing frequent terms from text data.
- Use BERTopic to visualize clusters of topics from IMDB reviews or news data.
- Discuss visual explainability: how do these visualizations build model trust?
๐ Homework (Due Monday, December 1st)
- Choose either a structured dataset (classification/regression) or an NLP text dataset from Hugging Face (ag = load_dataset("ag_news")).
- Produce:
- One feature importance plot.
- One model evaluation visualization (confusion matrix or ROC curve).
- One text visualization (word cloud, BERTopic topic cluster, or BERTopic Topic Word Scores).
- Include a 200โ300 word reflection on how visualization supports model interpretability.
- Submit
.ipynband.html.
๐งฉ Optional Extensions
- Create animated confusion matrices or topic evolution plots.
- Compare the performance of different classifiers.
โ Submission Checklist
Before submitting, make sure:
-
Your assignment has fulfilled all the basic requirements listed above.
-
Use Quarto to render the notebook into HTML and zip the files for submission.
-
Double-check the visualizations and your reflections in the HTML are properly organized and displayed.