Data Science

Ridge and Lasso Regression: L1 and L2 Regularization

Complete Guide Using Scikit-Learn

Saptashwa Bhattacharyya
September 26, 20187 min read

Moving on from a very important unsupervised learning technique that I have discussed last week, today we will dig deep in to supervised learning through linear regression, specifically two special linear regression model - Lasso and Ridge regression.

As I'm using the term linear, first let's clarify that linear models are one of the simplest way to predict output using a linear function of input features.

Linear model with n features for output prediction
Linear model with n features for output prediction

In the equation (1.1) above, we have shown the linear model based on the n number of features. Considering only a single feature as you probably already have understood that w[0] will be slope and b will represent intercept. Linear regression looks for optimizing w and b such that it minimizes the cost function. The cost function can be written as

Cost function for simple linear model
Cost function for simple linear model

In the equation above I have assumed the data-set has M instances and p features. Once we use linear regression on a data-set divided in to training and test set, calculating the scores on training and test set can give us a rough idea about whether the model is suffering from over-fitting or under-fitting. The chosen linear model can be just right also, if you're lucky enough! If we have very few features on a data-set and the score is poor for both training and test set then it's a problem of under-fitting. On the other hand if we have large number of features and test score is relatively poor than the training score then it's the problem of over-generalization or over-fitting. Ridge and Lasso regression are some of the simple techniques to reduce model complexity and prevent over-fitting which may result from simple linear regression.


Ridge Regression : In ridge regression, the cost function is altered by adding a penalty equivalent to square of the magnitude of the coefficients.

Cost function for ridge regression
Cost function for ridge regression

This is equivalent to saying minimizing the cost function in equation 1.2 under the condition as below

Supplement 1: Constrain on Ridge regression coefficients
Supplement 1: Constrain on Ridge regression coefficients

So ridge regression puts constraint on the coefficients (w). The penalty term (lambda) regularizes the coefficients such that if the coefficients take large values the optimization function is penalized. So, ridge regression shrinks the coefficients and it helps to reduce the model complexity and multi-collinearity. Going back to eq. 1.3 one can see that when λ → 0 , the cost function becomes similar to the linear regression cost function (eq. 1.2). So lower the constraint (low λ) on the features, the model will resemble linear regression model. Let's see an example using Boston house data and below is the code I used to depict linear regression as a limiting case of Ridge regression-

text
import matplotlib.pyplot as pltimport numpy as np import pandas as pdimport matplotlibmatplotlib.rcParams.update({'font.size': 12})
text
from sklearn.datasets import load_bostonfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LinearRegressionfrom sklearn.linear_model import Ridge
text
boston=load_boston()boston_df=pd.DataFrame(boston.data,columns=boston.feature_names)#print boston_df.info()
text
# add another column that contains the house prices which in scikit learn datasets are considered as targetboston_df['Price']=boston.target#print boston_df.head(3)
text
newX=boston_df.drop('Price',axis=1)print newX[0:3] # check newY=boston_df['Price']
text
#print type(newY)# pandas core frame
text
X_train,X_test,y_train,y_test=train_test_split(newX,newY,test_size=0.3,random_state=3)print len(X_test), len(y_test)
text
lr = LinearRegression()lr.fit(X_train, y_train)
text
rr = Ridge(alpha=0.01) 
text
# higher the alpha value, more restriction on the coefficients; low alpha > more generalization,# in this case linear and ridge regression resembles
text
rr.fit(X_train, y_train)
text
rr100 = Ridge(alpha=100) #  comparison with alpha valuerr100.fit(X_train, y_train)
text
train_score=lr.score(X_train, y_train)test_score=lr.score(X_test, y_test)
text
Ridge_train_score = rr.score(X_train,y_train)Ridge_test_score = rr.score(X_test, y_test)
text
Ridge_train_score100 = rr100.score(X_train,y_train)Ridge_test_score100 = rr100.score(X_test, y_test)
text
plt.plot(rr.coef_,alpha=0.7,linestyle='none',marker='*',markersize=5,color='red',label=r'Ridge; $alpha = 0.01$',zorder=7) 
text
plt.plot(rr100.coef_,alpha=0.5,linestyle='none',marker='d',markersize=6,color='blue',label=r'Ridge; $alpha = 100$') 
text
plt.plot(lr.coef_,alpha=0.4,linestyle='none',marker='o',markersize=7,color='green',label='Linear Regression')
text
plt.xlabel('Coefficient Index',fontsize=16)plt.ylabel('Coefficient Magnitude',fontsize=16)plt.legend(fontsize=13,loc=4)plt.show()
Figure 1: Ridge regression for different values of alpha is plotted to show linear regression as limiting case of ridge regression. Source: Author.
Figure 1: Ridge regression for different values of alpha is plotted to show linear regression as limiting case of ridge regression. Source: Author.

Let's understand the figure above. In X axis we plot the coefficient index and, for Boston data there are 13 features (for Python 0th index refers to 1st feature). For low value of α (0.01), when the coefficients are less restricted, the magnitudes of the coefficients are almost same as of linear regression. For higher value of α (100), we see that for coefficient indices 3,4,5 the magnitudes are considerably less compared to linear regression case. This is an example of shrinking coefficient magnitude using Ridge regression.


Lasso Regression : The cost function for Lasso (least absolute shrinkage and selection operator) regression can be written as

Cost function for Lasso regression
Cost function for Lasso regression
Supplement 2: Lasso regression coefficients; subject to similar constrain as Ridge, shown before.
Supplement 2: Lasso regression coefficients; subject to similar constrain as Ridge, shown before.

Just like Ridge regression cost function, for lambda =0, the equation above reduces to equation 1.2. The only difference is instead of taking the square of the coefficients, magnitudes are taken into account. This type of regularization (L1) can lead to zero coefficients i.e. some of the features are completely neglected for the evaluation of output. So Lasso regression not only helps in reducing over-fitting but it can help us in feature selection. Just like Ridge regression the regularization parameter (lambda) can be controlled and we will see the effect below using cancer data set in sklearn. Reason I am using cancer data instead of Boston house data, that I have used before, is, cancer data-set have 30 features compared to only 13 features of Boston house data. So feature selection using Lasso regression can be depicted well by changing the regularization parameter.

Figure 2: Lasso regression and feature selection dependence on the regularization parameter value. Source: Author.
Figure 2: Lasso regression and feature selection dependence on the regularization parameter value. Source: Author.

The code I used to make these plots is as below

Let's understand the plot and the code in a short summary.

  • The default value of regularization parameter in Lasso regression (given by α) is 1.

  • With this, out of 30 features in cancer data-set, only 4 features are used (non zero value of the coefficient).

  • Both training and test score (with only 4 features) are low; conclude that the model is under-fitting the cancer data-set.

  • Reduce this under-fitting by reducing alpha and increasing number of iterations. Now α = 0.01, non-zero features =10, training and test score increases.

  • Comparison of coefficient magnitude for two different values of alpha are shown in the left panel of figure 2. For alpha =1, we can see most of the coefficients are zero or nearly zero, which is not the case for alpha=0.01.

  • Further reduce α =0.0001, non-zero features = 22. Training and test scores are similar to basic linear regression case.

  • In the right panel of figure, for α = 0.0001, coefficients for Lasso regression and linear regression show close resemblance.


How Lasso Regularization Leads to Feature Selection?

So far we have gone through the basics of Ridge and Lasso regression and seen some examples to understand the applications. Now, I will try to explain why the Lasso regression can result in feature selection and Ridge regression only reduces the coefficients close to zero, but not zero. An illustrative figure below will help us to understand better, where we will assume a hypothetical data-set with only two features. Using the constrain for the coefficients of Ridge and Lasso regression (as shown above in the supplements 1 and 2), we can plot the figure below

Figure 3: Why LASSO can reduce dimension of feature space? Example on 2D feature space. Modified from the plot used in 'The Elements of Statistical Learning' by Author.
Figure 3: Why LASSO can reduce dimension of feature space? Example on 2D feature space. Modified from the plot used in 'The Elements of Statistical Learning' by Author.

For a two dimensional feature space, the constraint regions (see supplement 1 and 2) are plotted for Lasso and Ridge regression with cyan and green colours. The elliptical contours are the cost function of linear regression (eq. 1.2). Now if we have relaxed conditions on the coefficients, then the constrained regions can get bigger and eventually they will hit the centre of the ellipse. This is the case when Ridge and Lasso regression resembles linear regression results. Otherwise, both methods determine coefficients by finding the first point where the elliptical contours hit the region of constraints. The diamond (Lasso) has corners on the axes, unlike the disk, and whenever the elliptical region hits such point, one of the features completely vanishes! For higher dimensional feature space there can be many solutions on the axis with Lasso regression and thus we get only the important features selected.

Finally to end this meditation, let's summarize what we have learnt so far

  1. Cost function of Ridge and Lasso regression and importance of regularization term.

  2. Went through some examples using simple data-sets to understand Linear regression as a limiting case for both Lasso and Ridge regression.

  3. Understood why Lasso regression can lead to feature selection whereas Ridge can only shrink coefficients close to zero.

For further reading I suggest "The element of statistical learning"; J. Friedman et.al., Springer, pages- 79-91, 2008. Examples shown here to demonstrate regularization using L1 and L2 are influenced from the fantastic Machine Learning with Python book by Andreas Muller.

Hope you have enjoyed the post and stay happy ! Cheers !

P.S: Please see the comment made by Akanksha Rawat for a critical view on standardizing the variables before applying Ridge regression algorithm.


If you're interested in further fundamental machine learning concepts and more, you can consider joining Medium using My Link. You won't pay anything extra but I'll get a tiny commission. Appreciate you all!!

Join Medium with my referral link - Saptashwa Bhattacharyya

Related Articles