✎ Edit content·DAY 035 · POST 3 OF 5 · How It Works

Linear Regression

Machine Learning · 13 slides
DAY 035 · POST 3 OF 5
(REMINDER)
DAY 035
How Linear Regression Learns
@saurav_dnj_24github.com/SauravDnj · linkedin.com/in/sauravdnj
1 / 13

Theme

Palette

Download

4K — sharpest, slowest
🎬 Video options
Preparing preview…
Live preview · loops the “none” effect
All rendering runs in your browser. No server, no cost, no upload. MP4/WebM = full motion + effects · GIF = lightweight loop · PNG/PDF = static for the Instagram & LinkedIn carousel.

Caption (tap to copy)

📖 Deep dive (full written explanation)

The slides stay clean and scannable. Here's the in-depth explanation behind each one — great for the blog version, show notes, or studying the topic properly.
Slide 1 · How Linear Regression Learns

This is the mechanics post, where the model stops being a concept and becomes a concrete optimization procedure. The challenge is to make the math feel inevitable rather than arbitrary: every step follows from a single goal, minimizing total error. If a reader leaves understanding that 'fitting' is just minimizing a cost function, the post has succeeded.

The cover signals depth — this is the engine room. Readers who wanted the overview got it in posts 1 and 2; here they're ready for the loss function, the normal equation, and gradient descent.

Slide 2 · Define 'wrongness' first

Everything in training hinges on first defining what 'wrong' means quantitatively, and that's the cost function. For linear regression it's mean squared error: take each residual, square it, and average across all points. The whole training process is nothing more than finding the weights that make this single number as small as possible.

Framing MSE as 'a number that says how bad a given line is' demystifies optimization. The computer isn't doing anything mysterious — it's adjusting weights to push one scalar down. Once learners grasp that fitting equals minimizing MSE, gradient descent and the normal equation become two routes to the same destination rather than separate magic tricks.

Slide 3 · Why square the errors?

The choice to square errors is not arbitrary, and explaining it prevents it from feeling like a convenient hack. Squaring serves two purposes. First, it makes all errors positive, so misses above and below the line don't cancel out. Second, it penalizes large errors disproportionately: a miss of ten contributes a hundred to the cost, while a miss of one contributes only one.

This quadratic penalty shapes the model's behavior — it bends the line aggressively to avoid big misses, often at the cost of small ones. That's usually desirable, but it also explains why outliers can dominate the fit, which becomes a key trap in the 'Common Mistakes' post. Squaring also has the mathematical bonus of being smoothly differentiable, which is what makes both the closed-form solution and gradient descent possible.

Slide 4 · MSE in code

Translating MSE into a few lines of code closes the gap between formula and implementation. The function computes residuals, squares them, and averages — exactly the verbal definition, now executable. Seeing it run on a tiny example with a concrete output (0.4166...) makes the metric tangible.

This is also a quiet lesson in how simple the core of machine learning math really is. The intimidating Greek-symbol formula is three lines of numpy. Demystifying that builds confidence to read and write the optimization code that follows.

Slide 5 · Route 1: the normal equation

The normal equation is linear regression's special gift: a closed-form solution that gives the exact best-fit weights in one shot, no iteration required. The formula w = (XᵀX)⁻¹Xᵀy comes from setting the derivative of MSE to zero and solving directly. This is what scikit-learn uses by default, which is why fit() returns instantly.

It's worth being honest about its limit: inverting XᵀX becomes slow and memory-hungry when you have tens of thousands of features, and the matrix can be numerically unstable when features are collinear. That's precisely when practitioners switch to gradient descent. Knowing both routes — and when each applies — is what separates someone who uses the library from someone who understands it.

Slide 6 · The normal equation, by hand

Implementing the normal equation by hand cements that fit() is not magic. Adding a column of ones handles the bias term elegantly (the bias becomes just another weight, multiplied by a constant 1). Then a single line of linear algebra produces the full weight vector. Running this and comparing to scikit-learn's output is one of the most convincing exercises in early machine learning.

The code also surfaces a practical detail beginners miss: the bias trick. By augmenting the feature matrix with a ones column, the intercept folds neatly into the same matrix operation, so there's no separate special case. This pattern recurs throughout machine learning.

Slide 7 · Route 2: gradient descent

Gradient descent is the second route and the one that scales, so it deserves careful, intuitive treatment. The metaphor of rolling downhill on the cost surface is the right mental image: you stand somewhere on a bowl-shaped landscape (the MSE as a function of the weights), measure which direction is downhill (the gradient), and take a step that way. Repeat until you reach the bottom.

For linear regression the cost surface is a smooth bowl with a single minimum, so gradient descent is guaranteed to find the same answer as the normal equation. The reason to use it is scale: when you have millions of rows or features, the iterative approach is far cheaper than inverting a giant matrix. It's also the same algorithm that trains neural networks, so understanding it here pays dividends everywhere.

Slide 8 · The descent loop

The cycle diagram captures gradient descent as a repeating four-step loop: predict with the current weights, measure the cost, compute the gradient (the slope of the cost), and step the weights downhill. Drawing it as a cycle emphasizes that this is iterative — you go around the loop hundreds or thousands of times, each pass lowering the cost a little.

This loop is the heartbeat of nearly all modern machine learning training. Internalizing its shape here, on the simplest possible model, means it'll be instantly familiar when it reappears in logistic regression, neural networks, and beyond.

Slide 9 · Gradient descent step

The gradient descent code makes the loop concrete and runnable. Each iteration computes predictions, derives the gradient of MSE with respect to the weights (the (2/n)Xᵀ(pred − y) term), and nudges the weights in the downhill direction scaled by the learning rate. After enough iterations, the weights settle near the same minimum the normal equation would have found directly.

Seeing the gradient formula in code — rather than as an abstract derivative — helps demystify where it comes from. It's the slope of the squared-error cost, and stepping against it reduces error. The comment ties it back to the bowl metaphor: the weights end up 'near the MSE minimum.'

Slide 10 · The learning rate matters

The learning rate is the single most consequential hyperparameter in gradient descent, so it earns its own slide. It controls step size. Too small, and training crawls — you might need tens of thousands of steps to reach the bottom. Too large, and you overshoot the minimum, bouncing back and forth or even diverging to infinity as each step makes things worse.

The right value descends smoothly and converges in a reasonable number of steps. There's no universal correct number; it's found by experiment, often by trying values spaced by powers of ten. This intuition — small crawls, large diverges, somewhere in between is right — transfers directly to every gradient-based model the reader will ever train.

Slide 11 · Cost falling over steps

The bars diagram visualizes convergence: MSE starts high with random weights, drops steeply in the early steps, then flattens as the weights approach the minimum. This characteristic shape — fast initial progress, then a long slow tail — is what a healthy training run looks like.

Showing the curve teaches readers what to watch for in practice. If the cost isn't dropping, the learning rate may be too small or something is broken; if it's exploding, the rate is too large. Reading the loss curve is a core practical skill, and this is its first introduction.

Slide 12 · The mechanics, in order

The recap orders the mechanics into a clean sequence: define cost as MSE, square to punish big misses, then choose your solver — the normal equation for an exact one-shot answer on modest data, or gradient descent for an iterative, scalable answer on large data, with the learning rate controlling the step. Crucially, both routes reach the same minimum.

Consolidating this on one slide gives readers a durable summary of how linear regression learns. The 'both reach the same minimum' line is the reassuring punchline — there's one best line, and these are just two ways to find it.

Slide 13 · Save this. Follow for Day 36.

The CTA hands off to the hands-on build. Having seen the math from both angles, the reader is primed to assemble a full working pipeline. The next post loads real data, fits, reads coefficients, and evaluates end to end — the teaser promises the runnable payoff that makes the theory stick.

🎨 AI image prompt (matches this theme + palette)

Paste into Midjourney, DALL·E, Ideogram, etc. to generate an on-brand image, then upload it on the Edit content page. The prompt updates automatically with the selected theme + palette.