How do you select the regularization strength λ, and what does it mean to set it too high or too low?
I select λ with cross-validation on the training data, usually searching a logarithmic grid after scaling features inside the validation pipeline. A value that is too low permits large, unstable coefficients and overfitting; a value that is too high suppresses useful signal and causes underfitting.
How to think about it
I tune λ with cross-validation on the training data, usually over a logarithmically spaced grid, and select the value that gives the best validation performance for the metric the business actually cares about. Too little regularization leaves the model free to fit noise and produces unstable coefficients; too much shrinks away real signal and makes the model underfit.
Why λ changes the fit
Regularization adds a cost for large coefficients to the model’s training objective. For ridge regression, a common objective is:
J(β) = (1 / (2n)) Σᵢ (yᵢ − xᵢᵀβ)² + λ Σⱼ βⱼ²
Here, n is the number of training examples, β contains the model coefficients, and λ controls how strongly large coefficients are punished. Lasso uses the same idea but replaces the squared penalty with an absolute-value penalty:
J(β) = data loss + λ Σⱼ |βⱼ|
The mechanism is straightforward. When λ is small, reducing prediction error matters far more than keeping coefficients small. The model can assign a very large positive coefficient to one feature and a very large negative coefficient to another if that happens to reduce training error. With noisy or highly correlated features, that flexibility is often memorization wearing a tie.
When λ is large, coefficient size becomes expensive. The model accepts some training error to obtain a simpler, more stable function. That is the bias-variance trade-off:
- Bias is systematic error caused by a model being too constrained.
- Variance is sensitivity to the particular sample used for training.
A small λ generally means lower bias and higher variance. A large λ generally means higher bias and lower variance.
At λ equal to zero, ridge reduces to ordinary least squares, assuming the problem is solvable. At the theoretical limit of λ approaching infinity, the penalized coefficients approach zero. If the intercept is included and left unpenalized, the model becomes an intercept-only predictor: it predicts the training-target mean for every row.
That limit is useful for intuition, but it is not a practical setting. The useful value is usually somewhere in between, and the location depends on the data, feature scaling, loss definition, sample size, and model family.
A concrete example
Suppose I am predicting monthly apartment rent from 2,000 listings. The design matrix has 80 numeric and encoded features: floor area, bedrooms, building age, distance to transit, location indicators, and a few carefully chosen interactions. The production question concerns new listings from the same market and period, so assume the rows are sufficiently independent for ordinary shuffled five-fold cross-validation.
I standardize the penalized features inside each training fold and measure root mean squared error in dollars. A representative cross-validation report might look like this:
| λ | Training RMSE | Five-fold RMSE |
|---|---|---|
| 0.001 | $118 | $185 ± $6 |
| 0.01 | $122 | $172 ± $5 |
| 0.1 | $130 | $164 ± $4 |
| 1 | $141 | $158 ± $4 |
| 10 | $151 | $160 ± $3 |
| 100 | $201 | $179 ± $5 |
| 1,000 | $248 | $248 ± $7 |
The smallest average validation error occurs at λ equal to 1. The training error is lower at λ equal to 0.001, but its validation error is much worse. That is the classic low-regularization symptom: the model has enough freedom to explain quirks in the training listings that do not repeat in new listings.
At λ equal to 1, the model generalizes best in this report. At λ equal to 1,000, both training and validation errors are high, and predictions will usually be compressed toward the average rent. That is underfitting.
I would also consider the one-standard-error rule. The minimum validation RMSE is $158, with a standard error of $4, so the acceptance threshold is $162. The model at λ equal to 10 has an RMSE of $160 and is within that threshold. I might choose λ equal to 10 because it is more regularized and simpler while its measured performance is indistinguishable from the minimum within cross-validation uncertainty.
The choice is not “always pick the largest λ.” It is “prefer the simpler choice when the evidence says performance is effectively tied.”
How I would tune it in practice
I would search λ on a logarithmic grid rather than a linear one. Values such as 0.001, 0.01, 0.1, 1, 10, and 100 test multiplicative changes in strength. A linear grid from 0 to 100 would spend most of its points in a region that may be irrelevant and barely inspect the small values where the useful transition happens.
In scikit-learn, the parameter is commonly called alpha, even though it plays the role of λ here:
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV, KFold
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
alphas = np.logspace(-4, 4, 41)
pipeline = Pipeline([
("scale", StandardScaler()),
("model", Ridge())
])
cv = KFold(n_splits=5, shuffle=True, random_state=42)
search = GridSearchCV(
estimator=pipeline,
param_grid={"model__alpha": alphas},
scoring="neg_root_mean_squared_error",
cv=cv,
refit=True,
n_jobs=-1,
)
search.fit(X_train, y_train)
print(search.best_params_["model__alpha"])
The printed value is data-dependent. The negative scoring name is a scikit-learn convention: grid search maximizes scores, so it negates a loss such as RMSE. A less negative score means a smaller RMSE.
The pipeline matters. If I standardize all of X_train before cross-validation, the scaler has seen the validation folds while computing means and standard deviations. That is a small form of leakage. Putting scaling inside the pipeline fits it separately on each fold’s training portion.
The same selection pattern applies to lasso, replacing Ridge() with Lasso(max_iter=20000). The numeric value of the selected parameter can differ substantially between ridge and lasso because their penalties have different shapes.
What too low or too high looks like
With λ too low, the first observable symptom is often a widening gap between training and validation performance. In the rent example, training RMSE falls to $118 while cross-validation RMSE remains $185. Other warning signs include:
- coefficients that become very large;
- coefficient signs that flip across folds;
- predictions that are unusually extreme;
- validation error that gets worse as training error improves.
With lasso, a separate practical symptom can be a warning containing Objective did not converge. That usually means the optimizer reached its iteration limit before solving the requested problem accurately. Poorly scaled features, a difficult low-penalty problem, or an insufficient iteration limit can cause it. It is not evidence that the selected sparse model is good. I would scale the inputs, inspect the warning, and increase the iteration limit only after checking the data.
With λ too high, training and validation errors are both poor. Ridge coefficients become small, lasso coefficients become zero in greater numbers, and predictions cluster near the target mean. A rent model that predicts $2,400 for nearly every apartment may have admirable restraint, but it is not useful if the actual rents range from $1,100 to $5,000.
The senior-level nuance
The numerical value of λ is not portable. Feature units change it. A feature measured in dollars can have a coefficient around 0.01, while the same feature measured in thousands of dollars has a coefficient around 10. The predictions are identical, but the penalty on the coefficient is not. That is why feature scaling is normally part of the tuning pipeline.
Loss normalization changes it too. One implementation may penalize an averaged squared error, while another uses a summed error or a different constant factor. Some APIs expose the inverse of regularization strength rather than regularization strength itself. Therefore, λ equal to 1 has no universal meaning across libraries.
The validation split must match deployment. Random folds are reasonable for independent listings, but not if the same building appears in both training and validation. In that case I would group by building. If the model will predict future rents, I would use a time-based split so the model never learns from the future. A random split can produce an impressive score by quietly answering an easier question than the one production asks.
The metric must match the decision. RMSE heavily penalizes large misses, while MAE treats each absolute dollar error more evenly. If the business cares about expensive apartments, a weighted loss or a segment-level evaluation may matter more than the global average. I would not tune λ on RMSE and then claim the model is optimal for a business measured by the worst ten percent of errors.
Finally, selecting λ is not the same as proving the final score. If I repeatedly compare many grids, preprocessing choices, model types, and metrics against the same validation folds, I can overfit the validation process itself. For an unbiased performance estimate, I would use nested cross-validation or keep a genuinely untouched test set. After selecting λ, I refit the complete pipeline on all non-test data and evaluate the test set once.
What they’ll ask next
Why use a logarithmic grid?
Because useful regularization strengths often differ by factors of ten rather than by equal additive steps. If the best value is at the smallest or largest grid point, I would expand the grid instead of treating the boundary as the answer.
Why not choose λ on the test set?
Because choosing the value that performs best on the test set makes the test set part of model selection. Its final score then becomes optimistic. The test set is for the one-time estimate after tuning is finished.
Would you always choose the λ with the absolute lowest cross-validation error?
No. If several values are within the uncertainty of the minimum, I may use the largest such value under the one-standard-error rule. I would also inspect coefficient stability and deployment metrics, not just one decimal place of validation loss.
Say this in the interview
“I tune λ with training-only cross-validation on a logarithmic grid, with preprocessing inside each fold; a low value risks high-variance overfitting, while a high value shrinks away signal and causes high-bias underfitting.”