Lesson Introduction

This lesson provides a quick refresher on the core concepts of linear regression, focusing on key steps and implementation in Python using sklearn.

By the end of this lesson, you'll be ready to load datasets, split them, create and train a linear regression model, make predictions, and evaluate the model.

Loading Data

We'll start by loading the diabetes dataset from sklearn. This dataset contains ten baseline variables (age, sex, body mass index, average blood pressure, and six blood serum measurements), which were obtained for each of 442 diabetes patients. The target is a quantitative measure of disease progression one year after baseline.

import numpy as np
from sklearn import datasets

# Load the diabetes dataset
diabetes = datasets.load_diabetes()
X = diabetes.data  # Features
y = diabetes.target  # Target

print("Features:\n", X[:2])
print("Target:\n", y[:2])

Note that we can access features and target of this dataset by using .data and .target attributes.

This code prints out the first two rows of the dataset, so we can observe its structure:

Features:
 [[ 0.03807591  0.05068012  0.06169621  0.02187239 -0.0442235  -0.03482076
  -0.04340085 -0.00259226  0.01990749 -0.01764613]
 [-0.00188202 -0.04464164 -0.05147406 -0.02632753 -0.00844872 -0.01916334
   0.07441156 -0.03949338 -0.06833155 -0.09220405]]
Target:
 [151.  75.]

There is also a shortcut for loading X and y:

X, y = datasets.load_diabetes(return_X_y=True)

The return_X_y=True parameter allows us to split the dataset when loading. You can use any method you find comfortable.

Splitting the Dataset

Next, we'll split our data into training and testing sets, like we did before. As a reminder, we use the train_test_split function for it.

from sklearn.model_selection import train_test_split

# Split dataset into 80% training and 20% testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

print("Training set size:", X_train.shape)
print("Testing set size:", X_test.shape)

Output:

Training set size: (353, 10)
Testing set size: (89, 10)

The size of the test set, test_size, is set to 0.2, which is 20%. It is common to set the test set size to 20-30%,

Creating the Model
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal