datarekha
Patterns June 2, 2026

Regularization is a tax on complexity

Your model will always overfit given enough rope — regularization is the mechanism that prices complexity so the model only grows elaborate when the data actually demands it.

9 min read · by datarekha · machine-learningregularizationoverfittinggeneralizationlasso

A model with enough parameters will fit any training dataset perfectly. Given 100 data points and a polynomial with 100 free coefficients, you can thread a curve through every single point with zero residual error. You have not learned anything. You have drawn a connect-the-dots that happens to pass through noise as faithfully as it passes through signal. The first time that curve encounters a new data point — one it was never shown — it fails embarrassingly, because the noise it memorized was specific to this particular sample of the world and does not generalize to any other.

This is overfitting, and the standard fix is regularization: adding a penalty to the loss function that makes complexity itself expensive. The model has to justify each parameter. It can grow elaborate, but only if the data provides enough evidence that the elaboration is worth it.

That framing — paying a price for complexity — is the whole idea. Everything else is implementation detail.

The loss function already has a slot for this

Most supervised learning boils down to minimizing a loss function: some measure of how wrong the model’s predictions are on the training data. Mean squared error for regression, cross-entropy for classification. The loss is the cost of being wrong. Optimization makes the model less wrong.

The regularization insight is that you can add a second term to the loss, one that has nothing to do with prediction error and everything to do with the size of the model’s parameters:

total loss = prediction error + lambda * complexity penalty

The parameter lambda (sometimes written as alpha in sklearn) is the dial. Set it to zero and you are back to ordinary optimization with no regularization — the model can grow as complex as it wants. Set it to infinity and the penalty dominates, forcing every weight toward zero until you have a model that predicts the mean of the training set regardless of input. The useful range lives in between.

The prediction error pulls the model toward fitting the training data. The penalty pulls the model toward simplicity. The optimizer ends up at a compromise: a model that fits the training data well enough but is penalized for excessive complexity that is not backed up by strong signal.

This is Occam’s razor with a tunable dial. The philosophical principle — prefer simpler explanations — is here given a concrete, differentiable, optimizable form.

L2: the smooth shrink

Ridge regression, also called L2 regularization, defines complexity as the sum of squared weights:

L2 penalty = sum of (w_i squared)

Every weight contributes to this sum, so every weight is pushed toward zero during optimization. The larger a weight, the more it contributes to the penalty, and the stronger the push back toward zero. Large weights are expensive. The optimizer will only keep them large if they reduce prediction error by more than they cost in penalty.

The key behavior of L2 is that it shrinks all weights smoothly and proportionally. No weight ever reaches exactly zero unless the data provides zero evidence for it — they just get smaller. This is useful when you believe most of your features genuinely contribute something. L2 does not discard features; it dials them down.

Geometrically, the L2 penalty defines a sphere in weight space. The unconstrained minimum of the prediction error sits somewhere in that space. The regularized solution is the point where the sphere intersects the contours of the error surface — where the error is as small as possible subject to the constraint that the weights do not stray too far from the origin.

This geometry also explains why ridge regression tends to produce stable, well-conditioned solutions even when features are correlated. Correlated features confuse ordinary least squares — there are infinitely many weight combinations that give the same prediction — but the L2 penalty breaks the degeneracy by preferring the combination with smaller total weight.

Total Loss = Error + λ × Penaltyλ highsweetλ = 0Underfit(too simple)Good generalization(complexity justified by data)Overfit(memorizes noise)validation error (U-shaped)minimum— validation error- - training error← λ decreasingλ = 0 →
As lambda falls from high to zero, training error drops monotonically while validation error traces a U. The sweet spot is the minimum of the validation curve — where the data has justified the complexity.

L1: the sparse knife

Lasso regression, also called L1 regularization, changes the penalty from the sum of squared weights to the sum of absolute values:

L1 penalty = sum of |w_i|

This looks like a minor change. The consequences are dramatic.

L1 regularization drives some weights to exactly zero. Not very small — exactly, provably zero. A model trained with lasso can end up using 10 of its 100 features and assigning zero weight to the other 90. This is automatic feature selection. The model has decided, under the pressure of the penalty, that those 90 features are not earning their keep.

Why does L1 produce sparsity when L2 does not? The geometry is the explanation. The L2 constraint region is a smooth sphere. Where it touches the prediction error contours, the tangent is usually at a curved point, and curved points almost never sit exactly on a coordinate axis — so weights almost never land on exactly zero. The L1 constraint region is a diamond (in two dimensions) or a hyperdiamond (in higher dimensions), and it has sharp corners at the coordinate axes. The prediction error contours are far more likely to be tangent to one of those corners, and the corners are exactly the points where many weights are zero.

This geometric accident — a diamond has corners where a sphere does not — is why L1 regularization is used whenever you suspect most features are irrelevant and you want the model to identify which ones. Genomics, text analysis, any domain where you have ten thousand features and maybe a few hundred matter: lasso is the natural tool.

L2 is better when you believe most features matter a little but none should dominate. L1 is better when you believe a small subset of features explains nearly everything. In practice, elastic net — a convex combination of L1 and L2 penalties — lets you dial between the two behaviors and is often the pragmatic default.

L2 (Ridge)L1 (Lasso)OLS minRidge solutionw₁w₂Both weights non-zeroOLS minLasso solutionw₁w₂w₁ = 0 (corner solution)
L2 touches the error ellipse at a smooth curved point — both weights survive. L1’s diamond corner sits on a coordinate axis — one weight is driven to exactly zero. Same idea, different geometry.

What lambda is really doing

The penalty strength lambda is not a hyperparameter to be guessed. It is the formal expression of a prior belief about how complex the true underlying function is. A large lambda says: the world is simple; any complexity in the data is probably noise. A small lambda says: the world is complicated; the data’s complexity is probably real.

The right value is empirical, not theoretical. You train the model at many values of lambda — often on a log scale, because the interesting range spans orders of magnitude — and you evaluate each model on held-out validation data. The validation error traces a U-shaped curve. The left side of the U is underfitting: lambda is so large that the model is too simple to capture the real structure. The right side (low lambda) is overfitting: the model is memorizing training noise. The bottom of the U is where you want to be.

Cross-validation (splitting training data into folds and rotating which fold serves as validation) is the standard way to estimate where that bottom is without needing a separate validation set. In sklearn, RidgeCV and LassoCV do this automatically, fitting at multiple lambda values and reporting the best one.

One subtlety worth stating: you must normalize your features before regularizing. The penalty term punishes large weights. But whether a weight is large depends entirely on the scale of its corresponding feature — a weight of 0.1 on a feature that ranges from 0 to 10,000 is effectively tiny, while a weight of 0.1 on a feature that ranges from 0 to 1 is meaningful. If features are on different scales, the penalty will arbitrarily punish features on larger scales more. Standardize to zero mean and unit variance before regularizing, and the penalty is fair.

Why regularization is a generalization argument, not a fitting argument

Here is the subtler point that most introductions miss.

Regularization does not make the model fit the training data better. By construction, it makes the model fit the training data slightly worse — you are adding a term to the loss that has nothing to do with fitting the data. Regularized training loss is higher than unregularized training loss, always.

The reason to use regularization is entirely about the gap between training performance and test performance. Overfitting is not about having high training error; it is about having a large gap between low training error and high test error. Regularization shrinks that gap by constraining the model’s complexity, so that what the model learns is more likely to reflect the true structure of the problem and less likely to reflect the particular accidents of the training sample.

This is the connection to bias and variance. Regularization deliberately introduces a little bias — the model is no longer free to fit the training data perfectly — and in exchange buys a lot of variance reduction. The model’s predictions are more stable across different draws of training data. A regularized model trained on 1,000 different samples of the same underlying distribution will produce more similar predictions than an unregularized model trained on the same 1,000 samples. The unregularized model is highly sensitive to which particular data points happened to show up; the regularized model is not.

That stability is what generalization means. A model that generalizes well produces good predictions on data it has not seen before, because it learned the signal rather than the noise. Regularization is the mechanical implementation of that aspiration.

The practitioner’s mental model

When you are building a model and you notice training accuracy is high but validation accuracy is lower, the first question to ask is whether you have any regularization at all, and if so, whether lambda is calibrated. That training-validation gap is the signature of high variance — the model is fitting things it should not be fitting.

Increasing lambda squeezes that gap from the variance side. As you do, validation error falls — until you squeeze too hard and the model becomes too simple, at which point both training and validation error start rising together. That joint rise is the signature of high bias — the model’s assumptions are too tight to capture the real structure.

The sweet spot is not a fixed place. It moves with the size of your training data, because more data provides better evidence for complexity. A model that needs strong regularization with 500 training examples may need almost none with 50,000, because the data itself is providing enough information to distinguish signal from noise. This is why “add more data” and “add regularization” are both answers to the overfitting problem — they operate on different sides of the same equation.

Regularization prices complexity. Data provides the justification for complexity. The right model is the most complex one the data can afford.

What this means for the choices you make every day

Neural networks use weight decay — L2 regularization on the weights — as a default. Dropout, another neural network regularization technique, achieves variance reduction through a different mechanism (randomly zeroing activations during training) but the effect is the same: the model cannot rely on any single path through the network, so it learns redundant, distributed representations that generalize better.

Decision trees are regularized by constraining depth or minimum samples per leaf. Random forests and gradient boosted trees achieve variance reduction through ensembling — averaging many individually overfit trees produces a model with much lower variance. Every one of these is regularization under a different name: paying a price for complexity so that only justified complexity survives.

The tax metaphor is apt because taxes are not punishments — they are prices. A carbon tax does not forbid emissions; it makes emissions expensive, so the economy produces them only where they generate enough value to cover the cost. Regularization does not forbid complex models; it makes complexity expensive, so the optimizer produces it only where the data generates enough predictive value to cover the cost.

Complexity is not inherently bad. Unnecessary complexity is. Regularization is the mechanism that tells the difference.

Skip to content