This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

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

Import the necessary Python libraries first. For details, see Part I.

The Data: “diabetes” Dataset from Scikit Learn

Scikit learn comes with some standard datasets, one of which is the ‘diabetes’ dataset. In this dataset, ten baseline variables(features), age, sex, body mass index, average blood pressure, and six blood serum measurements(s1, s2, s3, s4, s5 and s6) were obtained for each of 442 diabetes patients, along with response of interest, a quantitative measure of disease progression (y) one year after baseline.

This dataset represents a classic regression problem, where the challenge is to model response y based on the ten features. This model can then be used for two purposes:

• one, to identify the important features (out of the ten mentioned above) that contribute to disease progression
• and two, to predict the response for future patients based on the features

Each of these ten feature variables has been mean-centred and scaled by the standard deviation times n_samples (i.e. the sum of squares of each column totals 1)

Let us get the diabetes dataset from the “datasets” submodule of scikit learn library and save it in an object called “diabetes” using the following commands:

In :

from sklearn import datasets

We now have loaded the data in the “diabetes” object. But in order to train models on it, we first need to get it in a format that is conducive and convenient for this process. In the next step, we do just that.

Data Preprocessing

The “diabetes” object belongs to the class Bunch, i.e. it is a collection of various objects bunched together in a dictionary-like format.

These objects include the feature matrix “data” and the target vector “target”. We will now create a pandas dataframe containing all the ten features and the response variable (diab_measure) using the following commands:

In :

df=pd.DataFrame(diabetes.data)
df.columns= diabetes.feature_names
# Creating a new column containing response variable ‘y’ (a quantitative measure of disease progressionone year after baseline)
df[‘diabetes_measure’]=diabetes.target

All the features and the response variable are now in a single dataframe object. In the next step, we will create the feature matrix(X) and the response variable(y) using the dataframe we just created:

In :

# Creating the feature matrix X
X=df.iloc[:,:-1]
# Creating the response vector y
y=df.iloc[:,-1]

Now that we have our feature matrix and the response vector, we can move on to build and compare different regression models. But in order to do that, we first need to choose a suitable methodology to evaluate and compare these models. In the next section, we do just that.

In the next installment, the author will go over a Model Evaluation Metric. 