Day 4 · 1 Hour
Data Visualization
matplotlib + seaborn — a chart that tells the story in 1 second that a table takes 5 minutes to read.
🔥 Real-World Hook (0–5 min)
NASA engineers had data showing O-ring failures at low temperatures before the Challenger disaster. The data existed — but it was in a table. When someone finally plotted failures vs. temperature the night before launch, the pattern was unmistakable. They didn't have time to act.
A good chart surfaces what tables hide. In ML, visualization catches distribution shifts, outliers, and class imbalance before they ruin your model.
📈 Core Chart Types (5–25 min)
| Chart | Use when | Example |
|---|---|---|
plt.hist() | See distribution shape | Score distribution |
plt.scatter() | Relationship between 2 variables | Height vs weight |
plt.bar() | Compare categories | Sales by city |
plt.plot() | Trends over time | Model loss vs epoch |
sns.heatmap() | Correlation matrix | Feature correlations |
sns.boxplot() | Distribution + outliers | Salary by role |
💻 Live Demo — AI Training Dashboard (25–45 min)
>import matplotlib.pyplot as plt import seaborn as sns import numpy as np import pandas as pd # Simulate model training history epochs = np.arange(1, 21) train_loss = 1.0 * np.e ** (-0.15 * epochs) + np.random.normal(0, .02, 20) val_loss = 1.0 * np.e ** (-0.12 * epochs) + np.random.normal(0, .03, 20) # 1. Training curves (most important AI chart) plt.figure(figsize=(10, 4)) plt.subplot(1, 2, 1) plt.plot(epochs, train_loss, label="Train Loss", color="#7c3aed") plt.plot(epochs, val_loss, label="Val Loss", color="#f59e0b", linestyle="--") plt.xlabel("Epoch"); plt.ylabel("Loss") plt.title("Training Curves"); plt.legend() # 2. Feature correlation heatmap df = pd.DataFrame(np.random.randn(50, 4), columns=["age", "income", "score", "churn"]) plt.subplot(1, 2, 2) sns.heatmap(df.corr(), annot=True, cmap="coolwarm", fmt=".2f") plt.title("Feature Correlations") plt.tight_layout() plt.savefig("dashboard.png", dpi=150) print("Saved dashboard.png")
📋 You Code It (45–55 min)
✍ Task
Plot a histogram of 500 random exam scores (normal distribution, mean=70, std=12). Add a vertical red line at mean. Title it "Score Distribution". Save as scores.png.
⚡ Day 4 Cheat Sheet
plt.figure(figsize=(w,h))Set canvas size
plt.subplot(r, c, i)Multi-chart layout
plt.hist(data, bins=20)Histogram
plt.scatter(x, y)Scatter plot
plt.axvline(x=mean)Vertical reference line
sns.heatmap(df.corr())Correlation heatmap
plt.savefig("out.png")Save to file
plt.tight_layout()Fix overlapping labels