Function Signature
Description
Themain() function in compare_models.py loads all saved model metrics from the results/ directory, compares them side-by-side in a formatted table, and identifies the best-performing model based on Test R² score.
How It Works
Input Requirements
A directory containing JSON files with model metrics. Each file must:
- Follow naming convention:
metrics_*.json(e.g.,metrics_linear_regression.json) - Contain metrics generated by
evaluate_model()function - Include at minimum:
model_name,train_r2,test_r2,train_rmse,test_rmse
Example Metrics File Structure
Output Format
The function displays a comprehensive comparison table:Ranking Criteria
Primary Metric: Test R² (Descending) Models are ranked by their Test R² score because:- Test R² measures performance on unseen data, indicating real-world generalization
- Higher values (closer to 1.0) indicate better predictive accuracy
- More reliable than Train R² for assessing true model quality
- Prevents selection of overfitted models that only perform well on training data
Running the Script
Command Line
From Python Code
Complete Workflow Example
Error Handling
Use Cases
- Model Selection - Quickly identify which model performs best on your dataset
- Experiment Tracking - Compare results across different hyperparameter configurations
- Documentation - Generate comparison tables for reports and presentations
- MLOps - Automate model selection in training pipelines
Related Functions
evaluate_model()- Generate metrics for individual modelsprint_metrics()- Display detailed metrics for a single model