![]() |
VOOZH | about |
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.
Output:
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.
The above code produce scatter plots of different independent variable with target variable as shown below
👁 scatter plots
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.
Output:
Let's plot a bar chart of above coefficients using matplotlib plotting library.
Output:
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:
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
Output: The value of MSE error and the dataframe with ridge coefficients.
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:
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
penalty.
Code : Python code implementing the Elastic Net
Output:
Bar plot of above coefficients:
Conclusion :
From the above analysis we can reach the following conclusion about different regularization methods: