Lasso Regularization (L1 Regularization) › Choosing
Machine Learning from ScratchAll chapters Lasso Regularization (L1 Regularization) › ChoosingOne section · Sections in this chapter7 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The regularization parameter is a hyperparameter, which is a value that is not learned from the data during model training but is set before training begins. The choice of is important:
- If is too small, the penalty is weak and the model may overfit (behave like ordinary least squares).
- If is too large, the penalty is too strong and the model may underfit (set too many coefficients to zero, losing important information).
To find the best , we typically use a process called hyperparameter search. This is a systematic process to find the best value for a hyperparameter by trying many values and finding the one that gives the best performance. The most common approach is cross-validation, which consists of the following steps:
- Define a range of candidate values (for example, from very small to quite large), e.g.
[0.01, 0.05, 0.1, 0.5, 1.0, 2.0] - For each value:
- Train the LASSO model on a subset of the data (the training set).
- Evaluate its performance on a separate subset (the validation set), using a metric like mean squared error (MSE).
- Select the that gives the best validation performance (i.e., the lowest error).
This process is often automated using tools like GridSearchCV in scikit-learn, which systematically tries many values and finds the optimal one.
Visualizing the Cross-Validation Process
To better understand how cross-validation helps us choose the optimal , let's visualize how model performance changes across different regularization strengths. The following plot demonstrates the bias-variance tradeoff that occurs as we vary .


This visualization reveals several important insights about choosing :
-
Training Error (Blue Line): Increases monotonically as increases. This is expected because stronger regularization constrains the model more, preventing it from fitting the training data as closely.
-
Validation Error (Red Line): Forms a U-shaped curve, which is the hallmark of the bias-variance tradeoff:
- Left side (small ): The model overfits the training data, leading to poor generalization and high validation error.
- Bottom (optimal ): The sweet spot where the model balances fitting the training data and generalizing to new data.
- Right side (large ): The model underfits, with too much regularization preventing it from capturing important patterns.
-
Feature Selection (Bottom Panel): Shows how the number of selected features decreases as regularization increases. The error-minimizing retains 8 features in this cross-validation run; with stronger regularization, the path later reaches the 3 truly important features marked by the horizontal dashed line.
-
Standard Error Bands: The shaded region around the validation error shows the variability across different cross-validation folds, helping us assess the stability of our performance estimates.
The optimal (marked by the green vertical line) minimizes the validation error. This is why cross-validation is the standard approach for hyperparameter selection in LASSO and other regularized models.