CodeBlanks Logo Icon CodeBlanks

Preparing data

Prepare data for regression.

Project: disease progression

In this project you'll predict the disease progression one year later (a numeric score) for 442 diabetes patients from age, sex, body mass index, blood pressure and 6 blood measurements. This is a regression problem.

from sklearn.datasets import load_diabetes
d = load_diabetes()
print(d.data.shape)          # (442, 10)
print(d.feature_names)       # ['age', 'sex', 'bmi', 'bp', 's1', ...]
print(d.target[:5])          # [151.  75. 141. 206. 135.]

The features in this dataset are pre-scaled: each column's mean is close to 0.

Getting to know the target

In a regression project, the target's range and distribution are needed to interpret the size of the error: an MAE of 50 means one thing if the target is in 25-350 and another if it is in 0-60.

print(y.min(), y.max())          # 25.0 346.0
print(round(y.mean(), 1), round(y.std(), 1))

As a baseline, the MAE of the "predict the mean for every patient" model is computed; the model must beat it.

Practice Training and test sets

Split the data with test_size=0.2, random_state=0. Print the training and test sizes on one line, then the mean of the training target with round(..., 1).

Starter code

model.py
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split

X, y = load_diabetes(return_X_y=True)

# buraya yaz

Sample solution

model.py
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split

X, y = load_diabetes(return_X_y=True)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
print(len(X_train), len(X_test))
print(round(y_train.mean(), 1))

Expected output

353 89
151.6

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which family of metrics should be used in this project?

  • Accuracy, F1
  • Confusion matrix
  • Precision, recall
  • MAE, RMSE, R² Correct answer

Explanation: The target is a continuous score, so regression metrics are used.

2 Set aside 20% of the data for testing.

Question
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=____, random_state=0)
Answer
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

Explanation: stratify isn't used in regression; there are no classes.

3 Match the facts.

Term Meaning
442 Number of patients
10 Number of features
bmi Body mass index
target One-year progression

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation