Welcome to your fifth lesson in our machine learning journey. In our last session, we dove deep into the mechanics of model training by deriving the gradient descent update rules for logistic regression. You now have a complete, from-scratch implementation of a classification model and a solid understanding of how it learns from data.
Today, we address a fundamental challenge in building predictive models: overfitting. A model that learns the training data "too well" often fails to generalize to new, unseen data. We will explore regularization, a powerful and widely-used set of techniques designed to prevent this. Specifically, you will learn to apply L1 and L2 regularization to a linear model. This will not only make our logistic regression model more robust but also introduce a key concept—controlling model complexity—that is vital for the advanced NLP models you aim to work with.
The Problem of Overfitting
When we train a model, we want it to learn the underlying patterns in the data, not the noise. If a model becomes too complex, it can essentially "memorize" the training examples, including their random quirks. This leads to excellent performance on the training set but poor performance on any new data. This phenomenon is called overfitting. In contrast, a model that is too simple might not even capture the underlying pattern, which is called underfitting.
A model's complexity is often related to the magnitude of its weights. Large weights can cause the model's output to change dramatically in response to small changes in the input features, leading to a jagged, unstable decision boundary that fits the training noise. The core idea of regularization is to discourage the learning algorithm from finding solutions with excessively large weights.
The following video provides an excellent high-level introduction to these concepts and sets the stage for how regularization helps find a better balance.
Machine Learning Tutorial Python - 17: L1 and L2 Regularization | Lasso, Ridge Regression
This video from the channel codebasics introduces the concepts of underfitting and overfitting and then explains how adding a penalty term to the cost function can help.
Please watch from the beginning until the end of the theoretical explanation. Pay close attention to how the cost function is modified to include a penalty for large parameter values (theta).
L2 Regularization (Ridge Regression)
The most common type of regularization is L2 regularization, also known as Ridge regression in the context of linear regression. It modifies the cost function by adding a penalty term proportional to the sum of the squares of the weight values.
Our original cost function for logistic regression (Binary Cross-Entropy) over samples was:
With L2 regularization, the new cost function becomes:
Let's break down the new term:
- is the vector of our model's weights. Notice that the bias term is typically not regularized.
- is the squared Euclidean norm (or L2 norm) of the weight vector, .
- (lambda) is the regularization hyperparameter. It controls the strength of the penalty. A larger means stronger regularization, forcing the weights to be smaller. If , we recover the original cost function.
Effect on Gradient Descent
Given your calculus background, we can see exactly how this changes the learning algorithm. To update the weights, we need the new partial derivatives. The derivative of the cost function with respect to a weight now includes a term from the penalty:
The gradient descent update rule for thus becomes:
We can rearrange this to see something interesting:
This shows that in every step, before we apply the regular gradient update, we first shrink the weight by a factor of . This is why L2 regularization is often called weight decay. It consistently pulls the weights towards zero, preventing any single weight from growing too large.
For a deeper analytical and probabilistic understanding, the following document explains L2 regularization as arising from placing a Gaussian prior on the model's weights in a Bayesian framework.
Understanding L1 and L2 regularization with analytical and ...
This document by Yuki Shizuya provides a detailed mathematical derivation for both L1 and L2 regularization, including both analytical and probabilistic viewpoints.
Please read Section 3, titled "L2 regularization." Focus on both the analytical derivation, which shows how a closed-form solution can be found, and the probabilistic derivation, which connects L2 to a Gaussian prior.
L1 Regularization (Lasso Regression)
A second, powerful type of regularization is L1 regularization, also known as Lasso (Least Absolute Shrinkage and Selection Operator). It differs from L2 by using the sum of the absolute values of the weights.
The L1-regularized cost function is:
Here, is the L1 norm of the weight vector, . While this looks like a small change, it has a profound effect: L1 regularization can shrink some weights to be exactly zero.
This means L1 can be used for feature selection, effectively removing irrelevant features from the model. This property of inducing sparsity is one of the most important differences between L1 and L2 regularization.
Geometric Intuition for Sparsity
Why does L1 produce sparse solutions while L2 does not? The reason lies in the geometry of the penalty terms. Imagine we are trying to minimize the loss function (the ellipses in the diagram below) subject to a constraint that the regularization term is less than or equal to some constant .
- For L1, the constraint is , which forms a diamond shape in two dimensions.
- For L2, the constraint is , which forms a circular shape.
The optimal solution, , is the point where the contours of the loss function first touch the constraint region.

As you can see, with the sharp corners of the L1 diamond, it is highly probable that the optimal point will lie on an axis (e.g., where ). With the smooth L2 circle, the contact point can be anywhere on the boundary, making it very unlikely that any weight will be exactly zero.
The following video provides a fantastic visualization of this key difference and shows how the penalty term's shape influences the final solution.
Ridge vs Lasso Regression, Visualized!!!
This StatQuest video visually contrasts how Ridge (L2) and Lasso (L1) penalties affect the cost function and the optimal parameters.
Watch the entire video. Pay close attention to the shape of the cost-plus-penalty curve for both Ridge and Lasso. Note how the "kink" at zero for the Lasso penalty is what allows it to shrink coefficients completely to zero.
Similar to L2, L1 regularization also has a probabilistic interpretation, which you can explore in the same document as before. It corresponds to placing a Laplace prior on the weights.
Understanding L1 and L2 regularization with analytical and ...
This is the same document as before.
Now, please read Section 2, "L1 regularization." The analytical derivation section discusses the non-differentiability of the L1 norm at zero, a key point given your math background. The probabilistic view connects L1 to the Laplace prior.
Applying Regularization in Practice
Now let's see how to implement this. We can modify our MyLogisticRegression class from the previous lesson, but in practice, we'll almost always use a library like scikit-learn.
Regularization in scikit-learn
The sklearn.linear_model.LogisticRegression class has built-in support for regularization. The key parameters are:
penalty: A string that can be'l1','l2','elasticnet', or'none'.C: A positive float that represents the inverse of regularization strength. This is a crucial convention: smallerCmeans stronger regularization (equivalent to a larger ).solver: Different optimization algorithms support different penalties. For example,'liblinear'supports both L1 and L2, while'lbfgs'only supports L2.
Here's how you would instantiate regularized models:
from sklearn.linear_model import LogisticRegression
# L2 Regularized Logistic Regression (default)
# C=1.0 is a moderate amount of regularization
l2_model = LogisticRegression(penalty='l2', C=1.0, solver='lbfgs')
# L1 Regularized Logistic Regression
# Note the change in solver
l1_model = LogisticRegression(penalty='l1', C=1.0, solver='liblinear')
The scikit-learn documentation provides a concise reference for the exact formulas used.
The official scikit-learn documentation is the definitive source for implementation details.
In the section on Logistic Regression, review the table in the subsection Binary Case, which clearly lays out the penalty terms corresponding to the penalty and l1_ratio parameters. Also, briefly look at the Lasso section to see its objective function.
The following image perfectly illustrates the effect of L1 regularization as the hyperparameter C is varied. On the left, you can see how the accuracy on the training and test sets changes. On the right, you see the values of the model's coefficients.

This clearly demonstrates the power of L1 for feature selection and the importance of choosing the right value for the regularization hyperparameter.
Conclusion
In this lesson, we've tackled the critical problem of overfitting and introduced regularization as a primary solution. You now have a solid theoretical and practical understanding of the two most common forms of regularization.
Here are the key takeaways:
- Overfitting occurs when a model learns the training data too well, capturing noise instead of the underlying pattern, which leads to poor generalization.
- Regularization prevents overfitting by adding a penalty term to the cost function that discourages large model weights.
- L2 Regularization (Ridge) uses a penalty based on the squared magnitude of the weights (). It shrinks weights towards zero, a process also known as weight decay.
- L1 Regularization (Lasso) uses a penalty based on the absolute value of the weights (). It can shrink weights to be exactly zero, thus performing implicit feature selection.
- The strength of regularization is controlled by a hyperparameter ( or its inverse, ), which must be tuned to find the best balance between underfitting and overfitting.
The need to tune the regularization hyperparameter sets the stage perfectly for our next topic. To find the best value for C, we first need a robust way to measure our model's performance. In the next lesson, we will explore a suite of evaluation metrics for classification tasks, such as precision, recall, and the F1-score, moving beyond simple accuracy to get a more nuanced view of model performance.