04.04 Lesson 4 of 16

1.1 h

Linear 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

Exercise

Compare regularized regressors

Use the Bike Sharing dataset from Module 01 to compare an unregularized linear model with Ridge and Lasso regression.

  1. Define a regression target and a feature matrix using cleaned Bike Sharing data.
  2. Build preprocessing inside a Pipeline and fit LinearRegression, Ridge and Lasso models.
  3. Search a small set of penalty strengths with cross-validation on training rows.
  4. Compare MAE and RMSE on the same validation rows.
  5. Inspect coefficient sizes and explain how Ridge and Lasso differ.
Done when

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

2 links
  1. 01
    Linear Models Article

    Read Ordinary Least Squares, Ridge, Lasso and Elastic Net. Focus on objective functions and penalty strength.

  2. 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

2 questions
  1. Question 1

    What is the main difference between Ridge and Lasso?

  2. Question 2

    What does increasing regularization strength usually do?

Completion check

Move on when

You can fit and compare linear, Ridge and Lasso models through a pipeline and explain their penalties and limitations.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels