Regularized Logistic Regression
Introduction
In this part, we will see how to implement regularized logistic regression
. Similar to regularized linear regression, we will modify logistic regression to prevent overfitting. Overfitting happens when the model is too complex and captures noise in the data, which leads to poor generalization to unseen examples.
Logistic Regression and Overfitting
We saw earlier that logistic regression can be prone to overfitting, especially when using high-order polynomial features. Let’s take a closer look:
In particular, fitting logistic regression with many features can lead to a complex decision boundary, which risks overfitting the training set. A simpler decision boundary that generalizes better is preferable.
Regularized Cost Function
We modify the cost function for logistic regression by adding a regularization term:
Here, λ
is the regularization parameter. Regularization discourages large weights by penalizing them in the cost function.
Minimizing this regularized cost function prevents parameters w_j
from becoming too large, allowing for better generalization to new examples.
Gradient Descent for Regularized Logistic Regression
Just like in regularized linear regression, we can update the weights and bias using gradient descent:
Note that the update rule for b
remains unchanged, as we do not regularize b
.
Implementing Regularized Logistic Regression
To implement regularized logistic regression, you apply the gradient descent update rules above. The only difference between regularized linear and logistic regression is that the prediction function f
is now the sigmoid of z
, where:
Intuition for Regularization
The regularization term shrinks the weights w_j
by a small amount on every iteration of gradient descent, which helps prevent overfitting.
This helps logistic regression generalize better to new examples.
Final Thoughts
By now, you’ve learned how to implement regularized logistic regression to reduce overfitting, even with a large number of features. You should practice this in the upcoming labs and apply regularization to avoid overfitting in logistic regression.
Congratulations on reaching the end of this section! There’s much more to learn, and in the next part, we’ll explore Neural Networks and their fascinating applications in Deep Learning.