# Building and Regularizing Linear Regression Models in Scikit-learn Contributor:
QuantInsti
Visit: QuantInsti

Excerpt

We will discuss the concept of regularization, its examples (Ridge, Lasso and Elastic Net regularizations) and how they can be implemented in Python using the `scikit learn `library.

Linear Regression problems also fall under supervised learning, where the goal is to construct a “model” or “estimator” which can predict the continuous dependent variable(y) given the set of values for features(X).

One of the underlying assumptions of any linear regression model is that the dependent variable(y) is (at least to some degree!) a linear function of the independent variables(Xis). That is, we can estimate y using the mathematical expression:

y=b0+b1X1+b2X2+b3X3+⋯+bnXn ,

where, /(b_i\)s are the coefficients that are to be estimated by the model.

The cost function that we seek to minimize is the sum of squared errors(also referred to as residuals). This methodology is called the ordinary least squares(OLS) approach. In other words, we want those optimal values of bis that minimize ∑(y−^y)2 , where ^y is the value of y predicted by the model.

Although the OLS approach works well in a lot of cases, it has its own drawbacks when the data has far-flung outliers or when the predictor variables (Xis) are correlated with each other. This can have a significant impact on the overall prediction accuracy of the model, especially for out of sample or new data. In such cases, some form of regularization is helpful. Two of the most popular regularization techniques are Ridge regression and Lasso regression, which we will discuss in this blog.

Importing Libraries

We will need some commonly used libraries such as pandas, numpy and matplotlib along with scikit learn itself: In :

import numpy as np
import pandas as pd
import matplotlib.pylab as plt
%matplotlib inline
import sklearn

That’s it!

In the next section, we will get some data to build our models on.