Topic 3 of 6 · 4 min read
Regularized linear models
Goal: Fit a regularized linear model and interpret its coefficients, including how feature scaling changes them.
A regularized linear model predicts with a weighted sum of your features and adds a penalty that keeps the weights small, which makes it stable and its coefficients readable. It is the baseline every tabular project should have.
Check yourself · 1 of 3
What does lasso do that ridge does not?
Why it matters
A model that predicts price as base + 40 * square_metres - 2000 * distance_km can be explained to anyone, audited line by line, and served in microseconds. Plain linear regression breaks when columns are strongly correlated or when there are many columns for few rows: the weights swing to huge opposite values and the model overfits. Regularization fixes that by penalising large weights.
The three models
- Ridge (L2 penalty) shrinks all weights toward zero but keeps them all. Good when many features each help a little.
- Lasso (L1 penalty) pushes some weights exactly to zero, so it selects features. Good when you expect only a few to matter. With a group of correlated features it keeps one more or less arbitrarily.
- Logistic regression is the classification version. Despite the name it predicts a class probability, and in scikit-learn it applies an L2 penalty by default. Its
Csetting is the inverse of the strength: smallerCmeans more regularization.
For ridge and lasso the strength is alpha; larger means a stronger penalty. Choose it by cross-validation with RidgeCV, LassoCV or LogisticRegressionCV.
Scaling changes the story
The penalty treats all weights equally, so a feature measured in large units (square metres: 20 to 300) needs a small weight while one in small units (rooms: 1 to 8) needs a large one. Unscaled, the penalty punishes the wrong feature. Standardize first, inside a pipeline. After standardizing, each coefficient means "the change in the prediction for a one standard deviation rise in this feature", so you can compare coefficient sizes across features. On raw units you cannot.
from sklearn.linear_model import RidgeCV
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
model = make_pipeline(StandardScaler(), RidgeCV(alphas=[0.1, 1, 10, 100]))
model.fit(X_train, y_train)
for name, w in zip(X_train.columns, model[-1].coef_):
print(f"{name:15s} {w:8.2f}")
What goes wrong
- Reading unscaled coefficients as importance. A tiny weight on a feature in large units can be a big effect.
- Treating a coefficient as causal. It describes association, holding the other features fixed, and shifts when correlated features are added or removed.
- Using lasso on correlated features and concluding the dropped ones do not matter.
- Forgetting that a linear model cannot see interactions or curves unless you add them as features.
Examples
Same model, different scale
A rent model gives area_m2 a coefficient of 12 and rooms a coefficient of 140. After standardizing, the two become 520 and 190. Area is the stronger driver; the raw numbers hid that because square metres have a much larger range than room counts.
Lasso and a twin feature
A dataset has weight_kg and weight_lb, which are the same signal. Lasso gives all the weight to weight_kg and zero to weight_lb. Concluding that pounds do not matter would be wrong: the information is there, under the other name.
Key points
- Ridge shrinks all weights; lasso sets some exactly to zero and so selects features.
- Standardize features inside a pipeline before ridge, lasso or logistic regression, or the penalty hits the wrong weights.
- In scikit-learn, logistic regression's C is the inverse of regularization strength, so a smaller C is a stronger penalty.
- Standardized coefficients can be compared across features; raw-unit coefficients cannot.
- A coefficient is an association with the other features held fixed, not proof of cause.
Common mistakes
- Fitting ridge or lasso on unscaled features.
- Comparing raw coefficients across features measured in different units.
- Reading a coefficient as the effect of intervening on that feature.
- Setting alpha by hand without cross-validation, then reporting it as the best value.
Check yourself · 2 of 3
In scikit-learn, you raise LogisticRegression's C from 0.1 to 10. What happens?
Check yourself · 3 of 3
A ridge model on unscaled data gives income (in dollars) a coefficient of 0.002 and age (in years) a coefficient of 1.5. What can you conclude?
Practice items, not counted towards your mastery.
Start here
- scikit-learn: ensembles (gradient boosting, random forests) (opens in a new tab)DocsFree
Reference for random forests and histogram gradient boosting, with the parameters that matter.
- XGBoost documentation (opens in a new tab)DocsFree
Official docs for the most used boosted-tree library, including parameter tuning notes.
- Why do tree-based models still outperform deep learning on tabular data? (opens in a new tab)PaperFree
The benchmark paper behind the claim that trees lead on medium-sized tabular data.
More for this topic
- scikit-learn: common pitfalls and recommended practices (opens in a new tab)ArticleFree
Shows data leakage from preprocessing before the split and how pipelines prevent it.