Model Evaluation
The Hidden Trap
Your model has 99% accuracy. Incredible, right? Wait. The dataset has 99% of one class:- 99% emails are not spam
- Model predicts “not spam” for everything
- 99% accuracy… but catches zero spam!
The Train-Test Split
Rule #1: Never evaluate on training data! Evaluating on training data is like grading a student using the exact questions they practiced on. Of course they’ll ace it — but you have no idea if they actually understand the material. The test set is the “final exam” your model has never seen.Cross-Validation: More Reliable Evaluation
What if the test split was “lucky”? Use k-fold cross-validation:Classification Metrics
The Confusion Matrix
Precision, Recall, F1
Precision
Of predicted positives, how many are correct?“Don’t cry wolf”
Recall
Of actual positives, how many did we find?“Find them all”
F1 Score
Harmonic mean of precision and recall“Balance both”
When to Use What?
Probability Thresholds
By default, we use 0.5 as the threshold. But you can adjust it:- Lower threshold (more sensitive): Catches more threats but also beeps at belt buckles. More positive predictions, higher recall, lower precision.
- Higher threshold (less sensitive): Only triggers on real weapons but might miss a hidden knife. Fewer positive predictions, lower recall, higher precision.
ROC Curve and AUC
The ROC curve shows performance across all thresholds:- 1.0 = Perfect model (always ranks positives above negatives)
- 0.5 = Random guessing (coin flip)
- > 0.9 = Excellent (production-ready for many applications)
- > 0.8 = Good (worth deploying with monitoring)
- > 0.7 = Fair (better than nothing, but investigate why it’s struggling)
- < 0.5 = Your labels might be flipped, or the model is actively anti-predicting
Regression Metrics
For predicting numbers:RMSE
Average error in same units as target.
More sensitive to large errors.
MAE
Average error in same units as target.
More robust to outliers.
R2 Score
% of variance explained (0 to 1).
1 = perfect fit, 0 = baseline.
MAPE
Average % error.
Easy to interpret.
Handling Imbalanced Data
When one class dominates (99% vs 1%):1. Use Appropriate Metrics
2. Resample the Data
Think of it like a cooking class where 95 students want to learn Italian but only 5 want to learn Thai. If you just teach to the majority, you’ll ignore Thai completely. Resampling either duplicates the Thai students (upsampling) or randomly removes some Italian students (downsampling) to give both groups fair representation.3. Use Class Weights
Learning Curves: Diagnosing Problems
Validation Curve: Tuning Hyperparameters
Complete Evaluation Pipeline
🚀 Mini Projects
Project 1: Metric Dashboard Builder
Build a comprehensive model evaluation dashboard
Project 2: Cross-Validation Analyzer
Compare different CV strategies and their stability
Project 3: Threshold Optimization
Find optimal decision thresholds for business needs
Project 4: Model Comparison Report
Create an automated model comparison report
Project 1: Metric Dashboard Builder
Build a comprehensive evaluation dashboard that calculates all metrics and visualizes model performance.Project 2: Cross-Validation Analyzer
Compare different cross-validation strategies and analyze their stability.Project 3: Threshold Optimization
Find the optimal classification threshold for different business objectives.Project 4: Model Comparison Report
Create an automated report comparing multiple models across all metrics.Key Takeaways
Never Evaluate on Training Data
Always use a held-out test set or cross-validation
Accuracy Is Not Enough
Use precision, recall, F1, AUC depending on the problem
Cross-Validation
More reliable than a single train-test split
Watch for Leakage
Test data must not influence training in any way
🧹 Real-World Complications: Messy Data Evaluation
Evaluating Models on Messy Data
Evaluating Models on Messy Data
What’s Next?
Before training, you need to prepare your data. Feature engineering can make or break your model!Continue to Module 8: Feature Engineering
Learn how to transform raw data into powerful features