Function Signature
Description
Theevaluate_model() function evaluates a trained machine learning model by computing multiple performance metrics on both training and test datasets. It also performs 5-fold cross-validation to assess model generalization.
Parameters
The trained machine learning model object. Must implement
predict() method (e.g., scikit-learn models).Training feature dataset. Typically a pandas DataFrame or numpy array containing the input features used to train the model.
Test feature dataset. Must have the same structure and feature columns as
X_train.Training target values. The actual house prices (or other target variable) corresponding to
X_train.Test target values. The actual house prices corresponding to
X_test, used to evaluate model performance.Human-readable name for the model (e.g., “Linear Regression”, “Decision Tree”). Used for identification in the metrics dictionary.
Return Value
Returns a tuple containing:
- Metrics Dictionary - Contains all computed performance metrics
- Test Predictions - The model’s predictions on the test set (
y_test_pred)
Metrics Dictionary Structure
Cross-Validation Details
The function performs 5-fold cross-validation on the training data:- The training set is split into 5 equal parts (folds)
- The model is trained on 4 folds and validated on the remaining fold
- This process repeats 5 times, with each fold serving as the validation set once
- The mean and standard deviation of R² scores across all folds are computed
- This provides a robust estimate of model generalization capability
Usage Example
Related Functions
print_metrics()- Display formatted metrics from this functioncompare_models()- Compare multiple models using saved metrics