Menu
CoddyTech

Diabetes Progress

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 55 of 58.

A diabetes clinic books follow-ups by how far each patient's disease will progress within a year. In section 2, one feature told only part of it:

Disease progression against scaled bmi for the clinic's patients, a wide cloud of dots with one orange line rising through it, labelled bmi alone: R squared 0.34

Now all ten measurements of load_diabetes go in. Which model should the clinic trust, and how many points off will it be?

It wants a fair pick by cross-validation, honest errors on unseen patients, and a residual check for anything the model misses.

challenge icon

Challenge

Hard

The program reads Ridge alphas into alphas; the starter sets up a titled chart.

  1. Split off a test quarter with random_state=0.
  2. Score a LinearRegression and a Ridge per alpha with 5-fold CV on the training rows. Print each mean R squared (3 decimals), then the winner.
  3. Fit the winner and print its test MAE and RMSE (1 decimal) and R squared (3).
  4. Scatter test residuals, true minus predicted, against the predictions. Add a zero line, then show.

For another clinic and alphas 0.5 and 2.0:

Linear: CV R squared 0.472
Ridge 0.5: CV R squared 0.481
Ridge 2.0: CV R squared 0.402
Winner: Ridge 0.5
Test errors: MAE 47.3, RMSE 58.9, R squared 0.414

Then Coddy describes your residual chart.

Try it yourself

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

diabetes = load_diabetes(as_frame=True)
X = diabetes.data
y = diabetes.target

# The Ridge alphas to try
n = int(input())
alphas = []
for i in range(n):
    alphas.append(float(input()))

fig, ax = plt.subplots()
ax.set_title("Residuals on Test Patients")
ax.set_xlabel("Predicted progression")
ax.set_ylabel("Residual (true - predicted)")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler