learning-it by example

Back to the video

Linear Regression From Scratch, Then scikit-learn

By hand and with scikit-learn, the slope came out at 5.77 both times. The true slope was 5.

Video #182 ยท Machine learning and data science

Steps in the video

  1. 0:00 Same slope, 5.77, by hand and by scikit-learn
  2. 0:30 The idea
  3. 1:08 Setup
  4. 1:33 The made-up data
  5. 1:58 Regression from scratch
  6. 2:33 The same with scikit-learn
  7. 3:02 Two checks and a slip
  8. 4:03 What this does not show
  9. 4:39 Mistakes to avoid
  10. 5:10 My take and next steps
  11. 5:43 Recap

Files

  • data.py (10 lines)
  • run.py (32 lines)
  • scratch.py (13 lines)

Download the code (zip)

Tested with

Python 3.12.15 | numpy==2.5.4, scikit-learn==1.9.1, scipy==1.18.1, joblib==1.6.0, narwhals==2.27.1, threadpoolctl==3.7.0, cloudpickle==3.1.2

What this does not show

Made-up data with one input and a truly straight line, scored on the same data it was fitted to.

Sources

Learning IT by Example.