04.04 Lesson 4 of 16
1.1 hLinear Models & Regularization
Use linear models as strong tabular baselines and control overfitting with Ridge, Lasso and Elastic Net penalties.
By the end of this lesson
You will be able to
- Explain the linear prediction equation for several features.
- Distinguish ordinary least squares, Ridge, Lasso and Elastic Net.
- Describe the bias-variance trade-off from regularization.
- Recognize when scaling is necessary before penalizing coefficients.
A linear model is a weighted sum
For a feature vector x, a linear regression model predicts ŷ = wᵀx + b. Each coefficient describes the model's change in prediction for a one-unit increase in that feature while other features are held constant. Ordinary least squares chooses coefficients that minimize squared residuals. Linear models can use many features, but they impose a linear relationship in the represented feature space.
from sklearn.linear_model import LinearRegression
model = Pipeline([
("prepare", preprocess),
("regressor", LinearRegression()),
])
model.fit(X_train, y_train)
predictions = model.predict(X_valid) Regularization penalizes large coefficients
Ridge adds an L2 penalty proportional to the sum of squared coefficients. It shrinks coefficients toward zero but usually does not set them exactly to zero. Lasso uses an L1 penalty and can produce exact zero coefficients. Elastic Net combines both. The strength parameter controls the trade-off between fitting training data and keeping coefficients small. Scale numeric features before comparing penalties; otherwise the penalty treats units unevenly. Tune the strength using validation or cross-validation, never the final test set.
Apply what you learned
ExerciseCompare regularized regressors
Use the Bike Sharing dataset from Module 01 to compare an unregularized linear model with Ridge and Lasso regression.
- Define a regression target and a feature matrix using cleaned Bike Sharing data.
- Build preprocessing inside a Pipeline and fit LinearRegression, Ridge and Lasso models.
- Search a small set of penalty strengths with cross-validation on training rows.
- Compare MAE and RMSE on the same validation rows.
- Inspect coefficient sizes and explain how Ridge and Lasso differ.
The comparison uses identical splits and preprocessing, tunes penalties without test data and reports predictive error plus the effect of regularization on coefficients.
Free references
Study resources
- 01 Linear Models Article
Read Ordinary Least Squares, Ridge, Lasso and Elastic Net. Focus on objective functions and penalty strength.
- 02 Regularization Part 1: Ridge (L2) Regression Video
Watch this individual Ridge explanation and note how the penalty changes coefficient values.
Check your understanding
Self-test
-
Question 1
What is the main difference between Ridge and Lasso?
Ridge uses an L2 penalty and shrinks coefficients, while Lasso uses an L1 penalty and can set some coefficients exactly to zero. -
Question 2
What does increasing regularization strength usually do?
It penalizes larger coefficients more strongly. This can reduce variance but may increase bias if the penalty is too strong.
Completion check
Move on when
You can fit and compare linear, Ridge and Lasso models through a pipeline and explain their penalties and limitations.