In this article, we will look into the implementation of different regularization techniques. First, we will start with multiple linear regression. For that, we require the python3 environment with sci-kit learn and pandas preinstall. We can also use google collaboratory or any other jupyter notebook environment.
First, we need to import some packages into our environment.
We are going to use the Boston house prediction dataset. This dataset is present in the datasets module of sklearn (scikit-learn) library. We can import this dataset as follows.
We can conclude from the above description that we have 13 independent variable and one dependent (House price) variable. Now we need to check for a correlation between independent and dependent variable. We can use scatterplot/corrplot for this.
We can observe from the above scatter plots that some of the independent variables are not very much correlated (either positively or negatively) with the target variable. These variables will get their coefficients to be reduced in regularization.
Code : Python code to pre-process the data.
Now, we apply train-test split to divide the dataset into two parts, one for training and another for testing. We will be using 25% of the data for testing.
Multiple (Linear) Regression
Now it’s the right time to test the models. We will be using multiple Linear Regression first. We train the model on training data and calculate the MSE on test.
As we can observe that lots of the variables have an insignificant coefficient, these coefficients did not contribute to the model very much and need to regulate or even eliminate some of these variables.
Ridge Regression added a term in ordinary least square error function that regularizes the value of coefficients of variables. This term is the sum of squares of coefficient multiplied by the parameter The motive of adding this term is to penalize the variable corresponding to that coefficient not very much correlated to the target variable. This term is called L2 regularization.
Code : Python code to use Ridge regression
The bar plot of above data is:
In the above graph we take = 1.
Let’s look at another bar plot with = 10
As we can observe from the above plots that helps in regularizing the coefficient and make them converge faster.
Notice that the above graphs can be misleading in a way that it shows some of the coefficients become zero. In Ridge Regularization, the coefficients can never be 0, they are just too small to observe in above plots.
Lasso Regression is similar to Ridge regression except here we add Mean Absolute value of coefficients in place of mean square value. Unlike Ridge Regression, Lasso regression can completely eliminate the variable by reducing its coefficient value to 0. The new term we added to Ordinary Least Square(OLS) is called L1 Regularization.
Code : Python code implementing the Lasso Regression
Output: The value of MSE error and the dataframe with Lasso coefficients.
The bar plot of above coefficients:
The Lasso Regression gave same result that ridge regression gave, when we increase the value of . Let’s look at another plot at = 10.
Elastic Net :
In elastic Net Regularization we added the both terms of L1 and L2 to get the final loss function. This leads us to reduce the following loss function:
where is between 0 and 1. when = 1, It reduces the penalty term to L1 penalty and if = 0, it reduces that term to L2
Code : Python code implementing the Elastic Net
From the above analysis we can reach the following conclusion about different regularization methods:
- Regularization is used to reduce the dependence on any particular independent variable by adding the penalty term to the Loss function. This term prevents the coefficients of the independent variables to take extreme values.
- Ridge Regression adds L2 regularization penalty term to loss function. This term reduces the coefficients but does not make them 0 and thus doesn’t eliminate any independent variable completely. It can be used to measure the impact of the different independent variables.
- Lasso Regression adds L1 regularization penalty term to loss function. This term reduces the coefficients as well as makes them 0 thus effectively eliminate the corresponding independent variable completely. It can be used for feature selection etc.
- Elastic Net is a combination of both of the above regularization. It contains both the L1 and L2 as its penalty term. It performs better than Ridge and Lasso Regression for most of the test cases.
- Lasso vs Ridge vs Elastic Net | ML
- Implementation of Elastic Net Regression From Scratch
- Implementation of Lasso Regression From Scratch using Python
- Implementation of Ridge Regression from Scratch using Python
- Multi-task lasso regression
- ML | Ridge Regressor using sklearn
- Bloom Filters - Introduction and Python Implementation
- Page Rank Algorithm and Implementation
- Implementation of Artificial Neural Network for AND Logic Gate with 2-bit Binary Input
- Implementation of Perceptron Algorithm for AND Logic Gate with 2-bit Binary Input
- Linear Regression (Python Implementation)
- Random Walk (Implementation in Python)
- Conway's Game Of Life (Python Implementation)
- Decision tree implementation using Python
- Python implementation of automatic Tic Tac Toe game using random number
- Implementation of K Nearest Neighbors
- Implementation of Dynamic Array in Python
- Interesting Python Implementation for Next Greater Elements
- Python | Implementation of Polynomial Regression
- Python | Implementation of Movie Recommender System
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.