Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

# What Is Meant by the Regularization of the ML Model?

Last Updated on July 17, 2023 by Editorial Team

#### Author(s): Mahesh Sonawane

Originally published on Towards AI.

## Regularization Models: Ridge-Lasso

When I started studying ML, I was unable to understand how Linear regression, Ridge, Lasso, and Gradient Descent work mathematically. When I ask doubts to my friends, everyone explains differently, and some of them only say, “Just do it on your ML models”. It is hard to understand those things without any graphical visualization. That is the reason I am writing an article on Ridge and Lasso. In this article, I tried to explain these concepts as simply as possible.

Prerequisite: Linear Regression, Read my article on Linear regression- (https://medium.com/towardsdev/concepts-behind-linear-regression-ml-model-8b6962074102). Before we start, I assume you know all the basic concepts of linear regression. Let's start.

## What is overfitting?

Let’s, take an example of a linear regression model.

Overfitting means the model tries to fit exactly to its training data. In the above example, the linear regression line goes through every point.

This means the accuracy of the overfitted model is 100%. Hence, the error is 0. (Error = Actual value — Predicted value = 0)

Assume your best fit line is Y = mx + c, Error = 0 (Error = Actual value — Predicted value = 0), m= tan(change in Y /change in X), c = 1 (intercept)

So, equation of line is Y= 0.03492076949 (X) + 1

look graph below, how will our model perform on unseen test data (Orange dots)? Errrrrrrrrrrrrrror…….

That means, Training accuracy = 100% and Testing accuracy = 70%

## How will you reduce overfitting?

Overfitting means the error is zero. Now to reduce overfitting, we have to allow the model to have some errors in fitting the model on training data. From the below graph, we can see the line is not fitting exactly to train data.

Performance of regularized model on testing data.

For the above generalized model, we get Train accuracy = 80% and Test accuracy = 75%. (compare this with overfit model accuracies)

Here, we see training accuracy reduces (which means we allow some error to make the model generalized); hence, testing accuracy increases on the generalized model.

## How did Ridge and Lasso introduce the extra amount of error in the model to avoid overfitting?

So assume the equation of the line is Y= 10 (X) + 1. Here, the coefficient is 0.03492076, and the intercept is 1. Look graph given below,

As the given model is overfitted,

## Ridge Regression (L2 Regularization)

Ridge regression adds some amount of error to the error function. So the model will not overfit.

Look carefully; Ridge regression added term shown in an orange box. The term is the multiplication of the sum of squared coefficients (B) and lambda. (lambda value can be chosen as per requirement)

The equation of the line is Y= 10 (X) + 1. Here, the coefficient is 10, and the intercept is 1.

Ridge regression achieves regularization by reducing the importance given to some of the features and not by nullifying the importance of the features. Hence, it does not cause sparsity.

## Lasso Regression (L1 Regularization)

Similarly, Lasso regression adds an error equivalent to lambda multiplied by the sum of absolute values of coefficients.

(lambda value can be chosen as per requirement)

Lasso regression achieves regularization by completely diminishing the importance given to some features (making the weight zero). Hence, causing sparsity

## Conclusion

Regularization basically aims at proper feature selection to avoid over-fitting. Proper feature selection is achieved by optimizing the importance given to the features. It mainly regularizes or reduces the coefficient of features toward zero.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI