Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 45

Building and Evaluating Regularized Linear Models

Recommended resources

Hello! Welcome to your next lesson in the Introduction to Data Science course.

In our last lesson, we completed a theoretical deep dive into L1 (Lasso) and L2 (Ridge) regularization. We explored their differences from analytical, geometric, and probabilistic viewpoints, establishing a firm understanding of why Lasso promotes sparsity and Ridge promotes smooth coefficient shrinkage.

Today, we put that theory into practice. This lesson is a hands-on project designed to fulfill your request for practical coding exercises that connect directly to mathematical foundations. Our goal is to build and evaluate a regularized linear model, analyzing the effect of the regularization parameter. You will implement Ridge regression from scratch, use it on a dataset, and visualize the bias-variance tradeoff in action by tuning the regularization strength, .

1. Setting the Stage: The Bias-Variance Tradeoff and Model Selection

Before we start coding, let's solidify the "why". The core purpose of regularization is to manage the bias-variance tradeoff. An unregularized, complex model (like a high-degree polynomial) might achieve very low error on the training data but fail to generalize to new, unseen data. This is overfitting, a state of high variance. A model that's too simple might fail to capture the underlying trend in the data, resulting in poor performance on both training and new data. This is underfitting, a state of high bias.

Regularization provides a way to control this tradeoff. By increasing the regularization parameter , we increase the model's bias (forcing it to be simpler) but decrease its variance. The goal is to find a "sweet spot" for that minimizes the total error on unseen data.

To do this properly, we need a robust way to select the best model (i.e., the best ) and get an unbiased estimate of its performance.

Lecture 8 - Data Splits, Models & Cross-Validation | Stanford CS229: Machine Learning (Autumn 2018)

To start, watch this segment from a Stanford lecture by Andrew Ng. It provides an excellent review of bias and variance, the effect of regularization, and the critical importance of splitting your data for model selection and evaluation.

Please watch the following segments: Bias, Variance, Underfitting, and Overfitting (04:56 - 12:29): This will review the core concepts and show examples of how model complexity affects the fit. Regularization (12:29 - 17:37): This explains how adding a regularization term helps prevent overfitting. Model Complexity vs. Error (37:51 - 40:57): Pay close attention to the U-shaped curve of generalization error. This is the exact curve we will aim to reproduce in our project. Train/Dev/Test Splits (40:57 - 01:02:02): This explains the standard methodology for hyperparameter tuning (like choosing \lambda) and getting an unbiased performance estimate.

The Mathematics of the Tradeoff

As you saw in the video, the test error curve follows a U-shape. This isn't just an empirical observation; it's grounded in a formal mathematical decomposition of the expected prediction error. For a given data point , the expected squared error of our model's prediction can be broken down into three components.

[PDF] Lecture 2: Overfitting. Regularization

To satisfy your request for mathematical rigor, please read the following sections from these lecture notes. They provide the complete derivation of the bias-variance decomposition.

Read the sections 'The anatomy of the error of an estimator' through 'Bias-variance decomposition (2)' (slides 33-38). Follow the derivation step-by-step. You will see how the expected error decomposes as: E_P[(y - h(\mathbf{x}))^2|\mathbf{x}] = \underbrace{(f(\mathbf{x}) - ar{h}(\mathbf{x}))^2}_{ ext{Bias}^2} + \underbrace{E_P[(h(\mathbf{x}) - ar{h}(\mathbf{x}))^2|\mathbf{x}]}_{ ext{Variance}} + \underbrace{E[(y - f(\mathbf{x}))^2|\mathbf{x}]}_{ ext{Noise}} Then, look at 'Error decomposition' (slide 39) to see a plot that visualizes exactly how these components change with model complexity (\lambda).

This decomposition is the theoretical underpinning for our project. When we plot our model's test error against different values of , we are experimentally tracing the (bias)^2 + variance curve.


2. The Project: Regularized Polynomial Regression

Now, let's start building. Our project will follow the structure of a typical machine learning task: data preparation, model implementation, training, and evaluation. We will use a simple dataset where a high-degree polynomial is likely to overfit, creating a perfect scenario to observe the effects of regularization.

The Model: Ridge Regression (Closed-Form Solution)

As we derived in a previous lesson, the cost function for Ridge Regression is:

where is the design matrix (which may contain polynomial features). Setting the gradient to zero gives us the closed-form solution:

Note that the identity matrix should have its first diagonal element set to zero to avoid penalizing the intercept term .

We will implement this equation directly using NumPy.

Ridge Regression From Scratch In Python [Machine Learning Tutorial]

This video provides a complete, step-by-step guide to implementing Ridge Regression from scratch in Python. It will be our primary guide for the coding portion of this lesson.

We will be following the logic presented in this video very closely. You can watch it in its entirety (about 30 minutes) to get a full overview, or you can refer to it as we go through the steps below. It covers: The theory and intuition behind Ridge Regression. The importance of standardizing your data. The full implementation in Python using NumPy. A method for finding the optimal regularization parameter (alpha in the video, lambda in our notation).


3. Step-by-Step Implementation Guide

This project will follow the structure laid out in the "Problem Set 3 Programming" resource, which provides a clear, academic framework for this task.

[PDF] Problem Set 3 Programming: Regularized Polynomial Regression

Please open the following PDF. It's a problem set from Harvey Mudd College that we will use as a structured guide for our project. It aligns perfectly with your goal of a rigorous, hands-on learning experience.

Familiarize yourself with the entire document. We will be completing the exercises in sections 2, 3, and 4 (Extra Credit). Pay special attention to the NumPy tips in the introduction.

Here is your task breakdown. You will need to write Python code to accomplish these steps. You can use a Jupyter Notebook or a simple Python script.

Step 1: Setup and Data Loading

  1. Load Data: Download the regression_train.csv and regression_test.csv files mentioned in the problem set PDF (or use any simple 2D dataset you can generate/find).
  2. Visualize: Plot the training and test data as scatter plots to get a feel for the data's shape.
  3. Data Splitting: You should have separate training and testing sets.

Step 2: Implement Polynomial Feature Generation

To create a situation where overfitting is likely, we need a complex model. A high-degree polynomial is perfect for this.

  1. Implement a function that takes a vector of x values and a degree m, and returns a design matrix of shape (n_samples, m+1). The columns should be . This corresponds to Part (g) of the problem set.
  2. For this project, we will use a fixed high degree, such as m = 10.

Step 3: Implement the Regularized Solver

This is the core of the project.

  1. Implement a fit function that takes the design matrix , the target vector , and the regularization parameter as input.
  2. Inside this function, calculate the coefficients using the closed-form Ridge solution: .
    • Remember to use numpy.linalg.inv for the matrix inverse and the @ operator or numpy.dot for matrix multiplication.
    • Crucially, create the penalty matrix lambda * I such that the intercept term is not penalized (i.e., the top-left element is zero).
  3. This task directly corresponds to Part (j) of the problem set.

Step 4: Analyze the Effect of

Now, we'll run the experiment to see how affects our model. This corresponds to Part (k) of the problem set.

  1. Define a range of values. A logarithmic scale is best, for example: [0, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1].
  2. Create a loop that iterates through your values. In each iteration:
    • Train your 10th-degree polynomial model on the training data using your regularized fit function.
    • Calculate the Root-Mean-Square Error (RMSE) on both the training data and the test data.
    • Store the resulting coefficients, the training RMSE, and the test RMSE.
  3. Plot the results:
    • Plot 1: Error vs. . Create a plot with log(lambda) on the x-axis and RMSE on the y-axis. Plot both the training error and the test error curves.
    • Plot 2: Coefficients vs. . Create a plot with log(lambda) on the x-axis and the coefficient values on the y-axis. Plot a line for each of the 11 coefficients to see how they shrink as increases.

Step 5: Interpretation

Analyze your plots to answer the following questions:

  • Describe the behavior of the training error and test error as increases. Where does the model appear to underfit? Where does it overfit?
  • Based on the test error curve, what is the best value for ?
  • How do the coefficient values change as increases? Does this match the theory of Ridge regression?
  • How do these results provide a practical demonstration of the bias-variance tradeoff?

Conclusion

Congratulations on completing this project! By building a regularized model from scratch and systematically evaluating its performance, you have bridged the gap between abstract theory and practical application. You have not only implemented a key machine learning algorithm but also reproduced one of its most fundamental concepts: the bias-variance tradeoff.

Key Takeaways:

  • Regularization is a powerful technique to control model complexity and prevent overfitting.
  • The regularization parameter directly controls the bias-variance tradeoff. A small leads to low bias and high variance (overfitting), while a large leads to high bias and low variance (underfitting).
  • Plotting training and validation/test error against is the standard method for finding the optimal level of regularization.
  • The closed-form solution for Ridge regression provides a direct and efficient way to train the model, leveraging your knowledge of linear algebra.

Preview of the Next Lesson:

We've spent considerable time on estimation from a frequentist perspective (MLE, OLS). The probabilistic view of regularization, however, gave us a glimpse into another powerful paradigm: Bayesian Inference. In our next module, we will formally dive into the Bayesian world. Our first lesson will introduce the core concepts, including how to derive the posterior distribution by combining a likelihood with a conjugate prior, starting with the classic example of a Gaussian likelihood and Gaussian prior.