What is model evaluation in machine learning?
Model evaluation is the process of measuring how well a trained model predicts unseen data and using those measurements to compare models, tune choices, and estimate production performance. It determines whether a model has learned a generalizable pattern rather than memorized its training data. Its main trade-off is that realistic evaluation requires representative held-out data and additional computation, whereas simpler evaluations may be fast but misleadingly optimistic.
Read full answerHide full answer
Applies tomachine learning concepts (examples in Python 3.12)
Evaluation Setup
A reliable evaluation separates data according to when information becomes available:
-
Training set: Fits model parameters.
-
Validation set: Supports model selection and hyperparameter tuning.
-
Test set: Remains untouched until all decisions are finalized, then provides a final performance estimate.
For limited data, cross-validation repeatedly trains on different folds and evaluates on the held-out fold:
-
KFold: Suitable for ordinary independent observations.
-
StratifiedKFold: Preserves approximate class proportions across folds and is commonly used for classification.
-
GroupKFold: Keeps observations from the same group in one fold, preventing group-level leakage.
-
TimeSeriesSplit: Uses earlier observations for training and later ones for validation, preserving temporal order.
Preprocessing that learns from data—such as scaling, imputation, encoding, or feature selection—must use only the training portion of each split. When a scikit-learn Pipeline is passed to cross-validation, each transformation is fitted only on that fold’s training data before being applied to its validation fold.
Classification Metrics
Let TP be true positives, TN true negatives, FP false positives, FN false negatives, and n the number of observations.
| Metric | Formula | Meaning |
|---|---|---|
| Accuracy | (TP + TN) / n |
Fraction classified correctly |
| Precision | TP / (TP + FP) |
Fraction of predicted positives that are correct |
| Recall | TP / (TP + FN) |
Fraction of actual positives detected |
| Specificity | TN / (TN + FP) |
Fraction of actual negatives correctly rejected |
| F1 | 2 * (Precision * Recall) / (Precision + Recall) |
Harmonic mean of precision and recall |
-
ROC curve: Plots
TPR = TP / (TP + FN)againstFPR = FP / (FP + TN)across decision thresholds. -
ROC-AUC: The area under the ROC curve; for distinct scores, it equals the probability that a random positive receives a higher score than a random negative. A value of
0.5corresponds to random ranking. -
Precision-recall curve: Plots precision and recall across decision thresholds.
-
PR-AUC: The area under the precision-recall curve; it is often more informative when positives are rare.
-
Log loss: For binary labels
y_iin{0, 1}and predicted probabilitiesp_i,-(1/n) * sum(y_i * log(p_i) + (1 - y_i) * log(1 - p_i)), wherelogis the natural logarithm. It penalizes confident incorrect predictions heavily. -
Brier score:
(1/n) * sum((p_i - y_i)^2), where lower values indicate better probability predictions. -
Reliability curve: Compares the mean predicted probability in each probability bin with the observed positive frequency, calculated as
positives_in_bin / observations_in_bin.
Accuracy can mislead on imbalanced data. If only 1% of cases are positive, always predicting the negative class achieves 99% accuracy while detecting no positives.
Regression Metrics
For n observations with actual values y_i and predictions y_hat_i:
| Metric | Formula | Interpretation |
|---|---|---|
| MAE | (1/n) * sum(abs(y_i - y_hat_i)) |
Average absolute prediction error |
| MSE | (1/n) * sum((y_i - y_hat_i)^2) |
Penalizes larger errors more strongly |
| RMSE | sqrt(MSE) |
Error in the target’s original units |
| R² | 1 - sum((y_i - y_hat_i)^2) / sum((y_i - mean(y))^2) |
Performance relative to predicting the evaluation-set mean |
| MAPE | (100/n) * sum(abs((y_i - y_hat_i) / y_i)) |
Percentage error; unstable near zero |
| Quantile loss | (1/n) * sum(q * max(y_i - y_hat_i, 0) + (1 - q) * max(y_hat_i - y_i, 0)) |
Pinball loss at quantile q, where q is between 0 and 1 |
MAE is less sensitive to outliers than RMSE. R² may be negative when a model performs worse than predicting the evaluation set’s mean. MAPE is undefined when an actual value is zero and unstable when values are close to zero.
Model Selection and Baselines
-
Start with a baseline, such as
DummyClassifierorDummyRegressor, to verify that the model adds meaningful predictive value. -
Use
GridSearchCVfor small parameter grids andRandomizedSearchCVfor large search spaces. -
Select metrics that reflect the cost of errors rather than relying only on accuracy or R².
-
Use nested cross-validation when tuning and reporting performance: inner folds select hyperparameters, while outer folds estimate the entire selection procedure.
-
Compare models with confidence intervals or paired statistical tests rather than treating small score differences as conclusive.
-
Keep the final test set untouched until model and threshold selection are complete.
Common Pitfalls
-
Data leakage: Validation or test information enters preprocessing or feature construction.
-
Test-set tuning: Repeatedly evaluating candidates on the test set inflates the final score.
-
Invalid splitting: Random splits break temporal or group relationships.
-
Wrong metric: The metric may not reflect the business objective or class imbalance.
-
Overfitting: A model may fit training data well but generalize poorly.
-
Distribution shift: Offline metrics may not reflect live traffic; production impact should be monitored or validated through A/B testing.
-
Ignored preprocessing requirements: Scaling matters for k-NN, SVM, PCA, and distance-based methods, but generally not for tree-based models.
Interview Tips
Be ready to explain how you selected the split, prevented leakage, chose metrics, established a baseline, and estimated uncertainty. Common follow-ups concern ROC-AUC versus PR-AUC, grouped or temporal validation, probability calibration, and why nested cross-validation gives a more honest estimate.