scikit-learn can fit a linear regression in one line. That's great for shipping — and terrible for actually understanding what just happened. So this is the version I wish someone had shown me first: linear regression built from scratch in about fifteen lines of NumPy, with the math explained as we go.

By the end you'll know exactly what "fit a line" means, where the slope and intercept come from, and how to measure whether the line is any good.

The idea in one sentence

Linear regression draws the straight line that sits as close as possible to all your points. "As close as possible" has a precise meaning: the line that makes the total squared vertical distance from the points to the line as small as it can be. That's the least squares method — and it has a clean closed-form solution, no training loop required.

We're fitting: