Skip to main content

Regularization: The Art of Keeping Models Simple

L1 L2 and Dropout Regularization

The Overfitting Problem

Remember: a model that memorizes training data is useless on new data.
  • Degree 1: Too simple (underfitting)
  • Degree 5: Just right
  • Degree 14: Wiggles through every point (overfitting)
GPT Training Regularization at OpenAI

What Is Regularization?

Core idea: Penalize complexity! Instead of just minimizing prediction error, we minimize: Loss=Prediction Error+λ×Model ComplexityLoss = \text{Prediction Error} + \lambda \times \text{Model Complexity} Where λ\lambda is the regularization strength. Trade-off:
  • λ = 0: No regularization, risk overfitting
  • λ = ∞: Maximum regularization, model predicts the mean
  • λ = just right: Balance fit and complexity

L2 Regularization (Ridge)

Add the sum of squared weights to the loss: LossRidge=MSE+λj=1pwj2Loss_{Ridge} = MSE + \lambda \sum_{j=1}^{p} w_j^2 Effect: Pushes weights toward zero, but never exactly zero. Creates “small” weights.

L1 Regularization (Lasso)

Add the sum of absolute weights to the loss: LossLasso=MSE+λj=1pwjLoss_{Lasso} = MSE + \lambda \sum_{j=1}^{p} |w_j| Effect: Pushes weights toward zero, and some become exactly zero. Creates sparse models!

Elastic Net: Best of Both Worlds

Combine L1 and L2: LossElasticNet=MSE+λ1wj+λ2wj2Loss_{ElasticNet} = MSE + \lambda_1 \sum |w_j| + \lambda_2 \sum w_j^2

When to Use Which?

Math Connection: L2 regularization is related to the Euclidean norm of the weight vector. L1 uses the Manhattan norm.

Regularization in Classification

For logistic regression:

Regularization in Tree-Based Models

Trees don’t use L1/L2, but they have their own regularization:

Dropout: Regularization for Neural Networks

Randomly “turn off” neurons during training:
Why it works: Forces the network to not rely on any single neuron. Creates redundancy.

Early Stopping

Stop training when validation performance stops improving:

Data Augmentation

Create more training examples by transforming existing ones:

Cross-Validation for Choosing λ


Regularization Summary


The Bias-Variance Tradeoff

Regularization is really about balancing:
The goal: Find the regularization strength that minimizes test error, not training error. Use cross-validation!

🚀 Mini Projects

Project 1: Regularization Comparison

Compare Ridge, Lasso, and ElasticNet

Project 2: Feature Selection with Lasso

Use L1 regularization to select features

Project 3: Overfitting Simulator

Visualize how regularization prevents overfitting

Project 4: Optimal Lambda Finder

Find the perfect regularization strength

Project 1: Regularization Comparison

Compare different regularization techniques on the same dataset.

Project 2: Feature Selection with Lasso

Use Lasso regularization to automatically select the most important features.

Project 3: Overfitting Simulator

Visualize how regularization prevents overfitting.

Project 4: Optimal Lambda Finder

Systematically find the best regularization strength using cross-validation.

Key Takeaways

Penalize Complexity

Add weight penalty to the loss function

L2 = Small Weights

Ridge shrinks all weights, none become zero

L1 = Zero Weights

Lasso creates sparse models, selects features

Cross-Validate λ

Always use CV to find the right regularization strength

What’s Next?

You now have a complete ML toolkit! Let’s see how to save, load, and deploy your models.

Continue to Module 14: Model Deployment

Learn how to save models and deploy them for real-world use

Interview Deep-Dive

Similar accuracy is not enough information to make this decision. I would ask several follow-up questions, but here is how I think about it:
  • Interpretability requirements. If stakeholders need to understand which features drive predictions — common in healthcare, finance, and compliance settings — the L1 model wins. Lasso produces sparse coefficients, so you can say “these 7 features matter, the rest do not.” L2 keeps all features active, making the explanation messier.
  • Feature stability over time. L1 models are sensitive to correlated features — if two features are highly correlated, Lasso will arbitrarily pick one and zero out the other. If feature availability or correlation structure changes in production, the L1 model may behave unpredictably. L2 is more stable because it distributes weight across correlated features.
  • Inference cost. If the L1 model zeroed out 80% of features, you only need to compute and transmit 20% of the features at inference time. At scale, this reduces latency and infrastructure cost. For a model serving millions of requests per day, fewer features means real savings.
  • Monitoring burden. Fewer active features (L1) means fewer things to monitor for drift. But it also means a single drifting feature has a bigger impact on predictions.
My default in production: if accuracy is truly similar and feature stability is not a concern, I lean toward L1 for the operational simplicity. But if the feature space has known multicollinearity, I would use ElasticNet or stick with L2.Follow-up: How does the choice change if you are working in a pipeline where feature computation is expensive?This strongly favors L1. If computing a feature requires a database query, an API call, or a complex aggregation, every feature you can eliminate from the model is infrastructure you do not need to maintain. I have seen production systems where the feature store was the bottleneck, not the model inference. In that scenario, a Lasso model that uses 10 features versus a Ridge model that uses 100 features can mean the difference between 20ms and 200ms prediction latency — and that is a business-critical difference.
I would use a hiring analogy. Imagine you are building a team to solve a problem:
  • L2 (Ridge) is like keeping everyone on the team but limiting how much each person works. Nobody gets fired, but everyone is told to contribute a little less. The result: a balanced team where everyone does a small part. The upside is stability — if one person calls in sick, others can compensate. The downside is that you are paying salary for people who contribute almost nothing.
  • L1 (Lasso) is like running a layoff based on performance. People who are not contributing get removed entirely. The team gets smaller and more focused. The upside is efficiency and clarity — you know exactly who matters. The downside is that if you fired the wrong person, there is nobody to cover for them.
Then I would connect it to the business context: “For our fraud detection model, I recommend L1 because it will tell us the 5 key signals that predict fraud, which the investigations team can act on. If I used L2, I would hand them a list of 50 factors, each with a tiny contribution, and that is not actionable.”Follow-up: How do you handle a product manager who says “just use both”?That is actually a real technique called ElasticNet. It combines both L1 and L2 penalties, and you tune a ratio parameter that controls how much of each you use. I would frame it as: “We can start with a blended approach, and the cross-validation process will automatically find the right balance between keeping all features and selecting the most important ones. The data will tell us the right answer.” This turns a binary decision into a continuous optimization, which is usually the right engineering instinct.
Time series adds several wrinkles that change how I think about regularization:
  • Temporal autocorrelation in features. Many engineered features in time series are lagged versions of each other (lag_1, lag_2, lag_7, etc.). These are highly correlated by construction. Pure L1 regularization will arbitrarily pick one lag and zero out others, which can make the model fragile if the most predictive lag shifts. I would default to ElasticNet here, or use L2 with aggressive feature selection as a separate preprocessing step.
  • Feature importance changes over time. The features that mattered last quarter may not matter this quarter. I would use a sliding-window retraining approach with regularization, and I would monitor whether the set of non-zero features (in L1) or the coefficient magnitudes (in L2) are stable across retraining windows. Large shifts signal regime change.
  • Multicollinearity from rolling statistics. If you engineer rolling_mean_7, rolling_mean_14, and rolling_mean_30, these are naturally correlated. Ridge handles this gracefully by sharing weight. Lasso will unpredictably drop some, which may break the model when the short-term vs long-term pattern changes.
  • The regularization strength should be tuned with TimeSeriesSplit, never random CV. This is critical. If you use random cross-validation to select lambda, you are letting future information influence the regularization choice, which inflates the perceived model quality.
In practice, for 200 time series features, I would first use Lasso to identify the top 30-50 features, then retrain with Ridge or ElasticNet on that reduced set. This two-stage approach gives you the sparsity benefit without the instability risk.Follow-up: How would you detect if your regularization is masking a data leakage problem in the time series features?The telltale sign is when a regularized model performs almost as well as an unregularized one on the test set. In a legitimate scenario with 200 features and noise, unregularized models should overfit badly. If they do not, it likely means some feature is leaking future information so strongly that even a simple model can exploit it. I would check feature importance and look for any feature with disproportionately high weight — especially any feature derived from rolling windows or aggregations that might accidentally include future timestamps.