Contents/ Part XVIII · Supervised Learning/ Chapter 116

Model Pitfalls

A model can post a great number and still be broken. This chapter is the field guide to the four traps that catch everyone: overfitting and underfitting, the bias-variance trade-off that explains them, imbalanced data and the SMOTE fix, and the cross-validation strategies that keep your estimates honest.

⏱️ ~19 min read
🐍 Notebook included
📊 Chapter 116

The gap between a demo and a deployed model is a short list of predictable failures. Each one produces a number that looks fine in isolation but hides a broken model, and each has a known diagnosis and fix. Knowing these four is what separates a practitioner from a beginner.

⚠️
Underfitting is a model too simple to catch the pattern (high bias); overfitting is one too complex, memorizing noise (high variance). The bias-variance trade-off is the tension between them. Imbalanced data lets a rare class be ignored, and the wrong cross-validation split gives a misleading score.
🧭
The chapter in one line

Diagnose before you fix: a train-versus-validation gap is overfitting, both errors high is underfitting, high accuracy with low minority recall is imbalance, and a leaky or unrepresentative split is a bad CV strategy.

1

Overfitting vs Underfitting

There are two ways for a model to be wrong, and they are opposites. Fit the same noisy data with a too-simple, a just-right, and a too-complex model and you can see both at once.

The same data, fit three ways underfit · high bias just right overfit · high variance

The straight line is underfitting, too rigid to bend to the real curve, so it is wrong everywhere (high bias). The wildly wiggly curve is overfitting: it passes through every training point, including the noise, so it fits the sample perfectly but will fail on anything new (high variance). The middle model captures the true shape without chasing the noise. The whole job of modeling is finding that balance.

2

The Bias-Variance Trade-off

Underfitting and overfitting are the two ends of a single dial, model complexity, and the trade-off between them is the most important idea in all of modeling. Plot error against complexity and the pattern is unmistakable.

Training error always falls; validation error is U-shaped model complexity → error underfit (high bias) overfit (high variance) training validation sweet spot

As complexity rises, training error keeps dropping, a flexible model can always fit the training points better, but validation error is U-shaped: high on the left where the model is too simple (bias), lowest at the sweet spot, and rising on the right where it overfits (variance). The bottom of the U is the model you want, and cross-validation is how you find it. A companion tool, the learning curve (error versus dataset size), tells you what to do about it: a wide, shrinking gap between the curves means more data will help (variance), while both curves high and together means it will not, you need a better model or features (bias).

3

Cross-Validation Strategies

Cross-validation is only trustworthy if the split respects the structure of your data. Plain shuffled k-fold is the wrong choice more often than beginners realize.

SplitterUse whenWhy
KFoldplenty of data, no structuresimple random folds
StratifiedKFoldclassification (esp. imbalanced)keeps each fold's class ratio representative
TimeSeriesSplittime-ordered datatrains on the past, validates on the future, no peeking ahead
GroupKFoldrepeated units (patient, customer)keeps a group's rows together, tests on unseen groups

The notebook shows why it matters: on the imbalanced defect data, plain KFold produces folds whose defect rate swings from 4% to 11%, a noisy, unreliable estimate, while StratifiedKFold holds every fold near the true 7%. And for time-ordered data, a random shuffle would let the model train on future rows and validate on past ones, leaking the answer; TimeSeriesSplit forbids it. Match the splitter to the data, or the score you report is a fiction.

4

Real-World Example: Imbalanced Data & SMOTE

A factory logs 1,200 units and wants to catch the roughly 7% that are defective. A standard classifier, chasing overall accuracy, quietly learns to call almost everything good, and misses nearly all the defects.

📂 Dataset · model-pitfalls--defects.xlsx

One row per unit with temperature, pressure, line_speed, machine_age_mo, and material_grade, plus the imbalanced label defective (about 7%).

ApproachRecall (defects caught)Precision
Plain classifier0.07 (misses almost everything)0.50
class_weight="balanced"0.820.19
SMOTE oversampling0.850.21

The plain model's 7% recall is a disaster hiding behind high accuracy. Two fixes rescue it. class_weight="balanced" makes minority mistakes count more, so the model pays attention to defects. SMOTE (Synthetic Minority Over-sampling Technique) generates plausible new defect examples between existing ones to balance the training set. Both lift recall past 0.8, trading some precision (more false alarms) for catching the defects that matter, and both must live inside the pipeline so they only ever touch the training fold, never the validation data.

5

Avoiding Pitfalls in Machine Learning & AI

These four traps do not disappear at scale, they get bigger, which is why the whole ML workflow is built to guard against them.

PitfallSymptomFix
Underfittinghigh training and validation errormore flexible model, better features
Overfittinglow train error, high validation errorregularization, more data, simpler model, ensembles
Imbalancehigh accuracy, low minority recallclass weights / SMOTE; judge by recall, not accuracy
Bad CV splitoptimistic or unstable estimatesstratified / time-series / group splitting
🤖
Why this matters for AI research

Every one of these scales up. Overfitting is the central challenge of deep learning, which is why regularization, dropout, early stopping, and data augmentation exist, they are all variance reducers. Imbalance is everywhere in the real world (fraud, disease, rare events), so reweighting and resampling are standard. And an improper validation split, letting the model see the future or the same entity in both train and test, is one of the most common reasons a published result fails to reproduce. The bias-variance lens is the single most useful diagnostic you can carry: whenever a model disappoints, ask first whether it is underfitting or overfitting, because the two demand opposite fixes.

🐍

See every pitfall in Python

The companion notebook fits under-, well-, and over-fit polynomials, draws the bias-variance validation curve and a learning curve, rescues an imbalanced defect model with class weights and SMOTE, and contrasts KFold, StratifiedKFold, and TimeSeriesSplit, every cell explained.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly. Open in Colab runs it live. To run locally, install numpy, pandas, scikit-learn, imbalanced-learn, matplotlib, seaborn, and openpyxl.

🎓 Key Takeaways

  • Underfitting (high bias) is too simple, both errors high; overfitting (high variance) is too complex, low train error but high validation error.
  • The bias-variance trade-off: training error always falls with complexity, but validation error is U-shaped, aim for the bottom.
  • Learning curves diagnose which you have: a shrinking train-validation gap means more data helps (variance); both high and close means it will not (bias).
  • Imbalanced data hides a rare class behind high accuracy; class_weight and SMOTE lift minority recall (0.07 → 0.85 on the defects).
  • Match the CV split to the data: stratified for classification, time-series for temporal, group for clustered, or the estimate lies.
6

Practice Challenges

Five short challenges. Try them with scikit-learn and imbalanced-learn before checking the solutions.

1

Underfit vs overfit

Fit a degree-1 and a degree-15 polynomial; compare training and test error.

Hint: low train + high test error = overfitting.
2

Find the sweet spot

Use a validation curve to pick the polynomial degree with the lowest validation error.

Hint: validation_curve(..., param_name="polynomialfeatures__degree").
3

Diagnose with a gap

Show a high-degree model has a large train-validation error gap.

Hint: compare training MSE to cross-validated MSE.
4

Rebalance a rare class

On the defect data, compare recall with and without class_weight="balanced".

Hint: recall should jump on the minority class.
5

Stratify the folds

Show StratifiedKFold keeps each fold's defect rate near the overall rate, but plain KFold does not.

Hint: print the mean of y in each fold.
Check your work

A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight quick questions on model pitfalls. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.

➡️
Up next

You can now build, evaluate, and debug a model, but can you explain why it predicts what it does? Model Interpretability & Explainability opens the black box with feature importance, partial dependence, SHAP, and LIME.