Classical machine learning
Regularization with L1, L2 and elastic net
Regularization adds a penalty for large weights. L2 shrinks weights smoothly, L1 drives some to exactly zero, and the penalty strength is tuned on validation data.
Without a penalty, a linear model can put huge weights on noisy or redundant features and overfit. Regularization adds a term to the loss that favors small weights. The model gives up a little fit on the training data in exchange for weights that hold up on new data.
The big idea
The training objective becomes:
loss = data loss + λ · penaltyHere λ (lambda) sets how strong the penalty is. There are three common penalties:
| Penalty | Extra term | Effect |
|---|---|---|
| L2 (ridge) | λ · sum of w² | Shrinks all weights smoothly. The default for most problems. |
| L1 (lasso) | λ · sum of abs(w) | Drives many weights to exactly zero, giving a sparse model. |
| Elastic net | A mix of L1 and L2 | Sparse and stable when features come in correlated groups. |
In scikit-learn the knob is usually C = 1/λ. A smaller C means stronger regularization.
An analogy: L2 is a pull toward zero that gets weaker as a weight gets smaller. L1 is a constant pull, so it can park a weight exactly at zero. The analogy stops at correlated features, where L1 picks one feature of a group and ignores the rest.
Walking through a collinear pair
Suppose two features are copies of each other, x₁ = x₂, and the target is y = x₁. Any weights with w₁ + w₂ = 1 fit the data equally well. The data cannot say which split is right.
L2 breaks the tie by choosing the smallest total of squared weights. Minimize w₁² + w₂² subject to w₁ + w₂ = 1, and the answer is w₁ = w₂ = 0.5. The weight is shared evenly.
L1 gives every split the same penalty, so the choice is arbitrary. In practice the solver tends to put all the weight on one of the two features.
This matters in real data. Two near-duplicate columns, such as spend over the last 30 days and spend over the last 31 days, can give wildly different coefficients from one resample to the next. Regularization makes the coefficients stable enough to read.
Choosing the strength
Pick λ (or C) on the validation set. Sweep it over a log grid, and plot the validation score against it. You usually see a U-shape:
- A tiny
Cunderfits, and most weights sit near zero. - A huge
Coverfits, and noisy features get large weights. - The validation minimum is your operating point.
Trade-off
Use L2 by default. It is stable and handles correlated features. Use L1 when you want an automatic shortlist of features for debugging, or a very small model. Use elastic net when you want sparsity but the features travel in correlated groups, such as one-hot levels of the same category.
Common mistakes
- Choosing
λon the test set. The test score then becomes optimistic. Tune on validation, and report test once. - Penalizing unscaled features. The penalty depends on the feature scale, so a feature in the millions gets treated very differently from one in single digits. Scale first. See feature scaling.
- Reading a zeroed L1 weight as "useless". The feature may be correlated with another feature that the model kept.
Check yourself
- Two identical features both fit
ywithw₁ + w₂ = 1. Which weights does L2 pick? Answer:w₁ = w₂ = 0.5, because that minimizes the sum of squares. - You decrease
C. What happens to the weights? Answer: The regularization gets stronger, so the weights shrink toward zero.
Key takeaways
- Regularization adds a penalty for large weights, which reduces overfitting.
- L2 shrinks weights smoothly and shares weight across correlated features.
- L1 can set weights to exactly zero, which gives sparse models.
- Tune the strength on validation data, never on test data.
Further learning
Next, make the features comparable before you penalize them: feature scaling and leakage-safe pipelines.
Related
Linear and logistic regression
The two models under almost everything in ML. They fit lines, predict probabilities and stay honest through regularization.
Logistic regression and log-odds
Logistic regression turns a linear score into a probability with the sigmoid and trains with log loss. Its coefficients read easily as odds multipliers.
Feature scaling and leakage-safe pipelines
Linear models and neural nets need features on comparable scales. Learn the scaling from training data only, and keep the scaler inside a pipeline so serving applies the same transform.