2.2.5 · D1Linear & Logistic Regression

Foundations — Normal equation closed-form solution

2,111 words10 min readBack to topic

This page is the ground floor of the parent topic. Before we can appreciate why is beautiful, we must earn every single symbol in it. We build from a single dot on paper up to the full matrix machine. If you can already read every line of the Equipment checklist at the bottom, skip ahead.


0. The scene: dots and a ruler

Imagine points scattered on graph paper. Each dot is one training example: something we measured (an input) paired with an outcome we want to predict (an output). Our whole job is to lay a straight ruler through them so it passes as close as possible to all the dots at once.

Figure — Normal equation closed-form solution

Look at the amber ruler above. The vertical cyan gaps between each dot and the ruler are the errors. "Best ruler" will mean: the ruler that makes those gaps, taken all together, as small as possible. Every symbol below exists to make that sentence precise.


1. A single number, a subscript, a superscript

Picture a spreadsheet. The subscript picks a column (which measured quantity), the superscript picks a row (which data point).

  • Why the topic needs it: with examples and features, we cannot write out every value. Subscripts and superscripts let us name any cell with two little labels.

2. Input , output , and how many of each

Picture: a table with rows and input columns, plus one final answer column .

  • Why the topic needs it: the parent's cost function sums — that "1 to m" is exactly "loop over every dot." You cannot read that sum without knowing .

3. Vector — a list of numbers with a direction

Figure — Normal equation closed-form solution

Two pictures of the same object: an arrow from the origin (left), and a stack of numbers (right). Both are . The arrow view helps for geometry (length, angle, perpendicular); the stack view helps for computation.

  • Why the topic needs it: (all the true answers) and (all the knobs) are vectors. Stacking them lets one formula act on the whole dataset at once.

4. — the knobs we tune

Picture the ruler again: sliding up/down moves the whole ruler vertically; changing tilts it. Finding the best ruler = finding the best .

  • Why the topic needs it: the normal equation's entire output is . It's the answer we solve for.

5. The prediction and the dot product

Picture: the dot product slides one vector along the other, multiplying overlapping entries — a single "weighted total." That total is the height of the ruler above that input point.

  • Why ? Notice needs a partner so the pattern stays uniform. We invent a fake feature for every example. Then , giving us the intercept for free while keeping one clean formula.

6. The error and the cost function

  • The is cosmetic: averages over dataset size, and the will cancel a factor of 2 when we differentiate. Neither changes where the minimum is.
  • Why the topic needs it: the normal equation is the exact place where bottoms out. is the thing we minimize.

7. Matrices, transpose, and the design matrix

Figure — Normal equation closed-form solution

The figure shows the shapes clicking together. Multiplying () by () gives : a single column of predictions — one for every dot, all at once. Matrix multiply is just "do the dot product of each row with the vector."


8. Gradient , the minimum, and "set to zero"

Figure — Normal equation closed-form solution

Because we squared the errors, plotted against is a bowl (a paraboloid) — the amber surface above. At the very bottom of a smooth bowl the ground is perfectly flat, so every slope is zero. That is why we write:

  • "Set the gradient to zero" means "find the flat spot," and for a bowl there's exactly one flat spot: the minimum. Solving that flatness condition is the normal equation.
  • Contrast: Gradient Descent walks downhill along in many small steps; the normal equation jumps straight to where in one algebra move.

9. Inverse, singular, and rank


Prerequisite map

Scalars and indices

Vectors as arrows and stacks

Dot product

Hypothesis h = theta dot x

Error and cost J

Matrices and transpose

Design matrix X

Vectorised cost

Gradient set to zero

Inverse and rank

Normal Equation

Every arrow says "you need the left box before the right box makes sense." The final box is the parent topic. Related roads out: Ridge Regression (fixes singular cases by adding to the diagonal), QR Decomposition (a numerically safer way to solve the same system), Feature Scaling and Polynomial Regression (how you build ), and Linear Regression Fundamentals for the big picture.


Equipment checklist

Cover the right side; can you answer each before revealing?

What does count?
The number of training examples (rows of data / dots).
What does count?
The number of features (input columns), not counting the added 1's column.
What is the difference between and ?
(parentheses) is the second example; is squared.
What does a bold lowercase symbol like mean?
A vector — an ordered column of numbers.
In one sentence, what is a dot product?
Multiply matching entries of two vectors and add them all up.
Why is there a fake feature ?
So gives the intercept while keeping one uniform dot-product formula.
Why do we square the errors in ?
To stop positive and negative gaps cancelling and to make a smooth single-minimum bowl.
What is the shape of ?
A column vector of predictions, one per example.
What does mean geometrically?
We are at the flat bottom of the cost bowl — the minimum.
When does fail to exist?
When is rank-deficient (a feature is a linear combination of others), making singular.
What does the transpose do?
Flips rows into columns; an matrix becomes .
Recall Self-check: can you read the whole formula aloud?

— "theta equals: transpose-X times X, all inverted, times transpose-X, times the answer vector y." If that sentence made sense, you're ready for the parent note.