2.2.14 · D1Linear & Logistic Regression

Foundations — L2 (Ridge) regularization

3,062 words14 min readBack to topic

This page assumes nothing. Before we can even read the Ridge cost function, we must earn every letter in it: what a weight is, what a vector is, what means, what a transpose does, what a matrix inverse is. Each symbol gets a plain-words meaning, a picture, and a reason the topic needs it. Read top to bottom — every idea rests on the one before.


1. A "feature" and a "weight" and the answer — the atoms

So there are two different 's to keep straight: is the truth (given in the data), is our guess (computed from weights). The whole game is making close to .

Picture a straight line through the origin. The weight is its slope — how steeply the prediction climbs as the feature grows.

Figure — L2 (Ridge) regularization

Why the topic needs this: Ridge is entirely a story about keeping small while stays close to . If you don't feel as a slope-dial that can grow dangerously large, the whole "charge a fee for big weights" idea has nothing to grab onto. Look at the red steep line in the figure — that is what "huge weight chasing noise" looks like.


2. The intercept — the line that need not pass through the origin

The line in Section 1 was forced through the origin (, so ). Real data rarely obeys that: even a house of "zero extra size" has some baseline price. We fix this with one more number.

Figure — L2 (Ridge) regularization

Why the topic needs this: The parent note says "the bias term is usually not penalized." You cannot understand that sentence without knowing what is and why it is special. From here on, assume the data has been centered, so we focus purely on the slope-weights .


3. Many features at once — the vector and

Real problems have many features per example (size and age and location...). We stack the features of one example into a list of numbers called a vector, written in bold: . Each is feature number , and is how many features there are.

The weights get their own vector — one dial per feature.

Picture as an arrow in space: in 2 features it's an arrow on a flat page, its two coordinates being (rightward) and (upward). The length of this arrow will turn out to be exactly what Ridge charges you for.

Figure — L2 (Ridge) regularization

Why the topic needs this: Ridge's penalty is "keep the weight arrow short." You cannot picture "short arrow" without first picturing the arrow.


4. Summation — "add up over all slots"

The parent note is full of . It is just shorthand for repeated addition.

Two uses appear constantly:

  • Over examples (where = number of training rows). Here = the true answer of the -th example, and = our prediction for it.
  • Over features .

Why the topic needs this: The data-fit term sums an error over all examples (); the penalty sums squared weights over all features (). Mixing them up breaks everything.


5. The squared error and the L2 norm — "size measured with squares"

Error of one prediction: using the symbols from Section 1, the error on example is — the gap between the true answer and our guess . We square it, , for two reasons: squaring makes it positive (a miss of is as bad as ), and it punishes big misses much harder than small ones.

Now the star of the show:

Picture the right triangle whose legs are and : the hypotenuse is , the arrow length. Squaring it is measuring the squared radius of a circle the arrow lives on.

Figure — L2 (Ridge) regularization

Why the topic needs this: The name "L2" in "L2 regularization" is this norm. The penalty is — literally " times the squared arrow length." Look at the figure: Ridge charges you the squared radius, so it hates long arrows enormously.


6. The dot product — the full prediction

With many features (and data centered so ), the prediction is feature-by-feature multiply, then add:

This pattern is so common it has a name: the dot product, written .

Why the topic needs this: Every appearance of in the parent is "the model's prediction for example ." It's just dressed in compact clothing.


7. The set and stacking data into a matrix

Before we build the data table, one tiny piece of notation.

We have examples, each a feature-vector. Stack them as rows of a table — that table is the design matrix , with rows and columns.

Picture as a spreadsheet, as the answer column beside it, and as the column of guesses you'd write next to them.

Why the topic needs this: The closed-form solution is written entirely in these matrix pieces. is the column of all errors; is the total squared error , using the row just defined.


8. The identity matrix and matrix inverse — undoing multiplication

To solve for we must "divide" by a matrix. Matrices don't divide; they invert.

Why the topic needs this: The whole closed form hinges on existing. This is the single most important thing buys you beyond shrinkage.


9. The lever — the fee rate

Picture a slider: push it right and the weight-arrow is forced shorter and shorter.

Figure — L2 (Ridge) regularization

Why the topic needs this: is the one dial the practitioner sets. Everything Ridge does — cure singularity, trade bias for variance, shrink noisy directions — is controlled by it. Look at how the arrow collapses toward the origin as grows.


10. The cost , its gradient, and convexity — why "derivative " works

Everything so far combines into a single number we want to make small: the cost (or loss) .

To find the smallest- weight, we use two more ideas.

Why the topic needs this: The Ridge cost is a sum of squares plus — a perfect bowl. That is why solving gives the answer directly, and why a unique closed form even exists.


Prerequisite map

feature and weight and answer

intercept baseline height

weight vector as arrow

L2 norm arrow length

dot product prediction

design matrix X and y

identity and inverse

cost function J

Ridge closed form

L2 Ridge Regularization

Read the map as: the atoms (feature, weight, answer) split into the intercept story on one side and the weight-arrow story on the other; the arrow gives us length (the penalty) and dot product (the prediction); those build the data matrix, the cost, and finally the closed form. From here, continue to the parent L2 (Ridge) regularization, and lean on Ordinary Least Squares (the case), Feature Scaling / Standardization (so the penalty is fair), Singular Value Decomposition (the deep shrinkage view), Bias-Variance Tradeoff and Cross-Validation (choosing ), while L1 (Lasso) regularization and Elastic Net contrast the penalty shape, and Logistic Regression reuses the same weight-shrinking trick.


Equipment checklist

Test yourself — reveal only after you've answered aloud.

What is a weight , pictured?
The slope of the prediction line; the dial saying how much a feature matters.
What is the intercept and how does Ridge treat it?
The baseline height of the line ( when all features are 0); Ridge does not penalize it — center the data instead.
What is the difference between and ?
is the true answer given in the data; is the model's computed guess at it.
What does bold represent geometrically?
An arrow in feature-space whose coordinates are the individual weights.
What does the index range over vs. the index ?
over features/weights (); over examples/rows ().
What is ?
The -th row of — the full feature-vector of example .
What does compute?
The squared L2 norm — the squared length of the weight arrow.
Why square the error and the norm?
Squaring makes them positive, punishes big values hard, and stays smooth/differentiable.
What is in words?
The dot product = multiply matching feature-weight pairs and add = the prediction .
What does the transpose do?
Tips a column vector onto its side so it can line up and multiply.
What does mean?
The set of tables of real numbers with rows and columns — i.e. the shape of .
What are , , and ?
Data table (rows=examples), true-answer column, and all predictions at once.
What is the cost function ?
A single number scoring how bad weights are: total squared error plus times squared norm.
Why can the or factor be dropped?
A positive constant times the whole cost doesn't move the minimum; it only rescales .
What is the identity matrix ?
The matrix version of : ones on the diagonal, zeros elsewhere; multiplying by it changes nothing.
Why does adding make invertible?
It lifts every diagonal by , re-inflating squashed directions into a positive-definite matrix.
What does control?
The strength of the weight-size fee: = OLS, = all weights crushed to .
Why can we solve Ridge by setting the gradient to zero?
The cost is convex (one bowl), so the flat spot () is the unique global minimum.

Recall Self-check: the parent's cost function, decoded

. Can you now read every symbol? over examples, the true answer, example 's row, = dot-product prediction , the square = smooth positive error, = fee rate, = squared arrow length. If yes — you're ready for the derivation.