Building and Regularizing Linear Regression Models in Scikit-learn – Part II

Articles From: QuantInsti
Website: 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)

Check out https://scikit-learn.org/stable/datasets for more information, including the source URL.

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 [34]:

from sklearn import datasets
diabetes = datasets.load_diabetes()

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 [35]:

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
df.head()

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 [36]:

# 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.

Visit QuantInsti Blog to download the ready-to-use code:
https://blog.quantinsti.com/linear-regression-models-scikit-learn/

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.