10.5: Overfitting and Generalization
- Page ID
- 48116
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
Overfitting and Generalization
A core challenge in machine learning is to build models that generalize well to new data – that is, they capture the underlying signal rather than just memorizing noise in the training data. Two key concepts in this regard are overfitting and underfitting, which relate to the bias-variance tradeoff. In this section, we explain these concepts and discuss strategies to manage the balance between a model’s complexity and its generalization performance.
Overfitting occurs when a model learns the training data too well – including its random fluctuations and noise – such that it performs poorly on unseen data [3]. An overfit model has low training error but high test error; it lacks the ability to generalize. Imagine a decision tree that grows unchecked: it may create many branches to perfectly classify each training example (even outliers), but those very specific rules won’t apply to new samples, causing errors. Overfitting is like memorizing answers to a practice exam – you get those exactly right, but you haven’t truly learned the material, so you might fail a different exam.
Underfitting, conversely, happens when a model is too simple to capture the underlying pattern of the data. It performs poorly on both training and test data (high errors on both), essentially failing to learn the signal. For example, fitting a straight line to data that is quadratic will underfit – the model is too biased (too rigid in form) to match the data.
The bias-variance tradeoff provides a lens to understand this. Bias is the error due to overly simplistic assumptions in the model (e.g., linearity); variance is the error due to sensitivity to small fluctuations in the training set (model complexity). As model complexity increases, bias tends to decrease (the model can fit more nuanced patterns), but variance tends to increase (the model becomes more sensitive to noise and specifics of the training data). The goal is to find the “sweet spot” of complexity where the sum of bias and variance (and irreducible noise) is minimized – this typically yields the best generalization performance.

In the figure above, a simpler model (far left) has high bias – it doesn’t fit the data well (both errors high). A very complex model (far right) has near-zero training error but a much larger gap between train and test errors, indicating high variance/overfitting. The optimum is somewhere in the middle where the test error is minimized. This conceptual figure guides us to manage complexity.
Techniques to prevent overfitting / improve generalization:
- Train with More Data: If possible, more training data can help the model discern signal from noise. With more examples, a complex model has to generalize across more variation, which can reduce overfitting. This isn’t always feasible, but data augmentation (especially for images, text, etc.) can effectively expand your dataset. - Feature Selection or Dimensionality Reduction: Eliminate irrelevant features (noise) or highly collinear features. Fewer inputs can simplify the model’s job. Techniques like removing features with low importance (for tree models) or using PCA (principal component analysis) to reduce dimensionality can help combat overfitting. - Regularization: This is a formal approach to penalize model complexity. For example, in linear and logistic regression, L2 regularization (Ridge) adds a penalty term to the loss, discouraging large coefficients (which tend to overfit noise). L1 regularization (Lasso) can even drive some coefficients to zero, performing feature selection. In decision trees, regularization can be imposed by limiting depth or requiring minimum samples for splits (preventing very specific rules). In neural networks, techniques like weight decay (similar to L2) or dropout (randomly dropping units during training) act as regularizers. Regularization essentially adds bias to reduce variance, leading to a model that might fit slightly less well on training data but generalizes better. - Cross-Validation for Model Selection: As discussed earlier, using cross-validation can help detect if a model is overfitting (e.g., if performance on folds varies widely or if very complex models don’t improve validation score). It guides us to choose an appropriate complexity (like the right degree of polynomial in regression, or the right tree depth) by seeing where additional complexity no longer yields better validation results. - Ensemble Methods: Methods like Random Forests (and boosted trees) inherently combat overfitting by averaging many models. The collective decision of an ensemble is often more robust than any single complex model. Ensembling tends to reduce variance. However, boosting methods can overfit if pushed too far, so they often include regularization as well (e.g., limiting tree depth or learning rate in gradient boosting). - Early Stopping: In iterative learning algorithms (like training a neural network or boosting models), one can monitor performance on a validation set and stop training when performance stops improving (or starts degrading) – essentially halt before overfitting. This is a form of regularization too. - Pruning (for trees): Grow a decision tree fully, then prune back branches that have little support or that do not generalize well (based on a validation set or statistical test). Pruning reduces variance by removing overly specific rules. - Data Augmentation and Noise Injection: Particularly in fields like image recognition or NLP, adding slight variations to inputs (rotating or flipping images, etc.) or injecting noise during training can force a model to be more tolerant to variations, thus generalize better.
It’s also important to monitor both training and validation metrics during modeling. If training error is much lower than validation error, that’s a classic sign of overfitting. If both are high, that indicates underfitting (perhaps a more complex model is needed or features are insufficient).
To illustrate with a concrete example: imagine fitting polynomial curves to a set of data points that truly follow a quadratic pattern with some noise. A linear model (straight line) underfits (high bias, misses the curvature). A quadratic model might fit well (low bias, low variance if enough data). A 10th-degree polynomial could exactly interpolate all training points (zero training error) but would wiggle wildly between them – likely a nonsense function for prediction (high variance). The bias-variance tradeoff conceptually explains this: low-degree polynomial = high bias, high-degree = high variance. The optimal complexity (degree ~2 in this case) minimizes generalization error.
In summary, generalization is the ultimate goal in predictive analytics models. All the techniques above are in service of finding a model that captures the real structure in data without getting distracted by random noise. This often requires a combination of good data practices, careful model selection, and applying regularization or ensembling as needed. Recognizing overfitting and underfitting from diagnostics and addressing them is a critical skill in building reliable predictive models.


