Lecture 9 · Interactive lab

Regression Lab

Fit a line by hand, watch outliers and extrapolation mislead, and predict real diabetes data

Introduction to Data Science and Machine Learning
Md Mahbub E Noor · University of Barishal
All labs

Fit a line by hand: how wrong is your line?

Move the slope and intercept. The red segments are the errors. Try to beat the best line, then reveal it and see how least squares finds it.

your MAE
your MSE

Your line, point by point (slide 5)

What to notice

  • Slope = marks gained per extra hour; intercept = predicted marks at 0 hours.
  • MAE averages the error sizes; MSE averages the squared errors, so big misses count much more.
  • LinearRegression finds the line with the smallest MSE. You can sometimes beat its MAE, but never its MSE.

Try this

Outliers and extrapolation: when a line misleads

Add one unusual student and watch the whole line swing. Then ask the line about 20 hours of study.

Ask the line

What to notice

  • Squaring the errors makes one far-away point pull the whole line towards it.
  • The shaded band is where we had data. Outside it, the line is a guess about a world we never saw.
  • Lines continue forever; real limits (100 marks, 24 hours a day) do not.

Try this

Real data: predicting diabetes progression

442 real patients, the same split as the lecture (test_size=0.25, random_state=0). Pick a feature and see how well one straight line predicts on the test patients.

test MAE
test R²
always predict the average

Every single feature, best first (homework 9.3)

What to notice

  • Patients with the same bmi differ a lot: one line can only capture the average trend.
  • R² = 0.16 means bmi explains about 16% of the variation. All 10 features together explain about 36%.
  • Every score here is on the test patients, which the line never saw.

Try this

What R² means: squares, not magic

R² compares your line with the laziest model, a flat line at the average. Each error is drawn as a square: R² = 1 − (your squares) ÷ (the flat line's squares).

The flat line: always predict the average

Your line

R²

What to notice

  • At tilt 0 your line is the flat line: R² = 0.
  • The best line (tilt 1) has the smallest total square area, so the highest R².
  • A line worse than the flat line has more square area: R² becomes negative.

Try this

Works offline: no internet needed. The data and the trained models are built into this page. Every number was calculated with scikit-learn and checked, so it matches what you will see in Jupyter or Colab.