# Regularization

In the previous article, when we were talking about feature selection, we mentioned that it may be difficult to determine ahead of time which features are important and which features are irrelevant. To solve this issue, many ML engineers use regularization. Instead of blindly picking and choosing features to use at the risk of losing important information, **it is often useful to just use all features but regularize them as well**. Thus, in implementation, regularization is applied as a safety measure by default and is applied to all features. Applying regularization by default also **safeguards against getting an overfitted model even though we are using all features**. To implement regularization, it is necessary to have a modified cost function. This looks different depending on what kind of regression it is.

### <mark>Linear Regression with Regularization</mark>

## **cost = J(w, b) = 1/2m(∑<sup>m</sup><sub>i = 1</sub>(f<sub>w,b</sub>(x<sup>(i)</sup>) - y<sup>(i)</sup>))<sup>2</sup> + <mark>λ/2m((∑<sup>n</sup><sub>j = 1</sub>(w<sub>j</sub>)<sup>2</sup>))</mark>**

Lambda is called the **regularization parameter**. It is a constant that we must choose (similar to the learning rate alpha). If lambda is 0, the model will overfit. If lambda is a large number, the model will underfit.

Thus, we have added an additional term to the mean-squared error cost function. When doing gradient descent, it is necessary to also minimize this added term. Note that by convention, we tend not to regularize the b parameter.

**<mark>Gradient Descent for Linear Regression with Regularization</mark>**

Gradient descent will also look different due to this added regularization term. Notice that the updating of the b term remains the same because we do not need to regularize b.

![](https://lh7-us.googleusercontent.com/sqbnbrOg3shDa3R1k9s38s9VojGKMlTjXBPeMfaSiGXXin23mUzpBY-5X1gg75S8V4N2RsjOp1Wen26xUVs_pWI7qul0HNEI1a7VypDrV_MZ7oJsmyGPG1h_79Z5xEX6-koQEs1VqQlk20kymHaJfPA align="left")

The added regularization term serves to shrink the value of w a little bit with each iteration. Over time, the w terms get shrunk significantly and their weight is reduced.

### <mark>Logistic Regression with Regularization</mark>

## **cost = J(w, b) = -1/m(∑<sup>m</sup><sub>i = 1</sub>y<sup>(i)</sup>log(f<sub>w,b</sub>(x)) + ((1-y)log(1- f<sub>w,b</sub>(x)))) + λ/2m((∑<sup>n</sup><sub>j = 1</sub>(w<sub>j</sub>)<sup>2</sup>))**

By reducing the weights of each feature, regularization with logistic regression ensures that the decision boundary is not overfitted to the data.

**<mark>Gradient Descient with Logistic Regression with Regularization</mark>**

The cost function to minimize is already given above. To implement gradient descent,

![](https://lh7-us.googleusercontent.com/sqbnbrOg3shDa3R1k9s38s9VojGKMlTjXBPeMfaSiGXXin23mUzpBY-5X1gg75S8V4N2RsjOp1Wen26xUVs_pWI7qul0HNEI1a7VypDrV_MZ7oJsmyGPG1h_79Z5xEX6-koQEs1VqQlk20kymHaJfPA align="left")

This looks the same as the gradient descent formulas for linear regression with regularization; the only difference is that the definition of f<sub>w, b</sub> is not wx + b but is now the sigmoid function g(z) where z = wx + b.

<details data-node-type="hn-details-summary"><summary>note</summary><div data-type="detailsContent">The images in this article are from Andrew Ng's Machine Learning Specialization on Coursera.</div></details>
