A model is only as good as its evaluation. This final chapter is about measuring performance honestly — the metric traps that fool beginners, why models that ace training can fail in the wild, and what happens after a model works.
Beyond accuracy
Accuracy alone lies on imbalanced data. If 98% of transactions are legitimate, a model that predicts "legit" every time is 98% accurate and completely useless — it never catches fraud. You need metrics that account for the errors that matter:
Classification metrics
| Metric | Answers |
|---|---|
| Precision | Of predicted positives, how many were right? |
| Recall | Of actual positives, how many did we catch? |
| F1 score | The balance of precision and recall |
| Confusion matrix | The full breakdown of right/wrong per class |
from sklearn.metrics import classification_report, confusion_matrixpreds = model.predict(X_test)print(confusion_matrix(y_test, preds))print(classification_report(y_test, preds)) # precision, recall, F1 per class
Overfitting
A model overfits when it memorises the training data — including its noise — instead of learning the general pattern. The tell: great on training, poor on the test set. The opposite, underfitting, is too simple to capture the pattern at all:
- Overfit → gather more data, simplify the model, or add regularisation.
- Underfit → use a richer model or better features.
Cross-validation
A single train/test split can get lucky. K-fold cross-validation splits the data k ways, trains and tests k times, and averages — a far more reliable estimate of real-world performance:
from sklearn.model_selection import cross_val_scorescores = cross_val_score(model, X, y, cv=5) # 5 foldsprint(f"Mean accuracy: {scores.mean():.2%} (+/- {scores.std():.2%})")
Shipping a model
A model in a notebook helps no one. Getting value from it means:
- Save the trained model and serve predictions behind an API (see the Node.js / AWS courses).
- Monitor it — real-world data drifts, so performance decays over time.
- Retrain on fresh data periodically.
Practice
Re-evaluate your model from Chapter 3 with a classification_report and confusion matrix instead of raw accuracy. Run 5-fold cross-validation. If training accuracy far exceeds test accuracy, note that it's overfitting and one thing you'd try.
Key takeaways
- 01Accuracy misleads on imbalanced data — use precision, recall, F1 and the confusion matrix.
- 02Choose the metric by which error is more costly (recall vs precision).
- 03Overfitting = memorising training data (great train, poor test); underfitting = too simple.
- 04K-fold cross-validation gives a more reliable performance estimate than one split.
- 05A useful model is served, monitored for drift, and retrained on fresh data.
