🌏 中文版
Video status: Videos included. This article is written from the official MOOC handout 09_handout.pdf and transcripts of this lecture's four official videos, with formulas following the handout. The transcripts are machine transcripts made for this article (Whisper), not official captions, and no human has proofread them. The videos are taught in Mandarin; the slides are in English. Each section opens with its official video and the approximate time ranges. Page numbers refer to the number at the lower right of each slide (0/23 to 23/23), and wherever the text says "in the lecture", it is paraphrasing what is said in the videos.
This is Foundations Lecture 9 in Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. On the Roadmap on page 1 of the handout, this lecture begins the third of the four big questions: How Can Machines Learn? The conclusion of the previous question takes up one line on the same page: learning can happen as long as there is a target distribution and can be made low with respect to the chosen error measure err. That is the conclusion of Foundations Lecture 8: Noise and Error Measures. The lecture opens with one more remark: the VC bound, proved with so much effort for binary classification, carries over to other error measures and to noisy settings, regression included, so attention can now go to how the algorithm is designed.
The prerequisites are matrix multiplication, transposes, inverses, partial derivatives, and, from earlier lectures, , , and the perceptron hypothesis . After reading, you should be able to write in matrix form on your own, derive the gradient, solve for , and run the whole algorithm by hand on a small data set; to say what the hat matrix does and where the two learning-curve formulas come from; and to explain why the regression solution can be used directly for classification.
The output changes from "approve or not" to "how much credit"
(Video 00:18–02:05 sets up the problem, 02:05–04:01 covers the hypothesis, 04:01–06:41 the illustration and residuals, 06:41–09:10 the error measure, and Fun Time follows.)
The credit limit problem
The running example in earlier lectures was a bank deciding whether to issue a credit card, with only two possible outputs. Page 2 of the handout changes it to the Credit Limit Problem: for the same customer (23 years old, female, annual salary NTD 1,000,000, 1 year in residence, 0.5 year in job, current debt 200,000), the bank now has to decide how large a limit to give, and the answer on the slide reads 100,000. In the lecture, Lin explains that this is no longer a yes/no question: this customer might get NTD 100,000 and another might get NTD 80,000.
The learning flow diagram is the same as before: the unknown target function (the ideal credit limit formula), the training examples (historical records in the bank), the hypothesis set , the algorithm , and the final . The only difference is written at the bottom of the page:
The output space is all of the real numbers, and problems of this kind are called regression. The lecture adds a side note: a credit limit can only be positive, since a bank does not hand out a negative limit, but it is more convenient to take a wider view of the output space and treat it as all of the reals.
The hypothesis: a perceptron without the sign
Page 3 picks the simplest hypothesis. Write the customer's features as , where is the constant term added by convention, and approximate the desired credit limit with a weighted sum:
The handout's note is "like perceptron, but without the sign". The perceptron (Foundations Lecture 2: Perceptrons and PLA) computes the score and then takes its sign to answer circle or cross; linear regression computes the score and outputs it as is, because the score is already a real number.
Page 4 uses two figures to show what this hypothesis looks like:
| Left figure: | Right figure: | |
|---|---|---|
| Axes | Horizontal axis , vertical axis | The base plane is , , the height is |
| Data points | A cloud of circles in the plane | A cloud of circles in space |
| Hypothesis | A blue line | A blue plane |
| Red segments | The vertical distance from each point to the line | The vertical distance from each point to the plane |
The lecture points out that this differs from the earlier figures: a classification figure draws only the plane of and uses circles and crosses for ; here takes an axis of its own and every data point is drawn as a circle. The red segments are called residuals, and in the lecture Lin refers to them as the "remainder": the part that is still unexplained after the hypothesis has made its prediction. The handout sums up the job of linear regression in one line: find lines/hyperplanes with small residuals.
The error measure: squared error
Whether the residuals are small needs a way of measuring. Page 5 uses the squared error, which the handout calls the popular/historical error measure:
In the lecture, Lin says linear regression is a problem the statistics community has studied for a long time, and the squared error is the measure traditionally used most. Plugged into the framework of the previous lecture, the in-sample and out-of-sample errors are
Two details of notation are both addressed in the lecture. First, each corresponds to one , so the handout writes directly rather than , which keeps later formulas a little simpler. Second, the expectation in is taken over together: we are already in the noisy setting of the previous lecture, where and are drawn jointly from one distribution.
This article adds a small example to show what squaring does (the numbers are made up for this article, not from the slides). Work in units of NTD 10,000: predict 9 when the actual value is 10, and the error is ; predict 12 when the actual value is 8, and the error is . The gap is 4 times larger and the error is 16 times larger, so the squared error cares especially about points that are far off. This property comes back in the last section, when regression is used for classification.
The last line of this page is "next: how to minimize ?". In the lecture, Lin explains that if you believe the derivation of the VC bound, and believe it extends to other problems, then this is the only thing left to do.
Fun Time: which feature should get a positive weight
Question (page 6): Consider using the linear regression hypothesis to predict the credit limit of customers. Which feature below shall have a positive weight in a good hypothesis for the task?
- birth month
- monthly income
- current debt
- number of credit cards owned
Answer: 2. The handout's explanation: customers with higher monthly income should naturally be given a higher credit limit, which is captured by the positive weight. A positive means that each one-unit increase in raises the predicted limit by .
The handout does not explain the other options; what follows is this article's reasoning. Birth month has no visible relation to the ability to repay, so a good hypothesis gives it a weight close to 0. The more current debt, the less room there is to borrow more, so the reasonable weight is negative. The direction for the number of credit cards is unclear: holding many cards may mean good credit, or it may mean a lot has already been borrowed, and nothing in this question says it has to be positive.
Set the gradient to zero and solve for the best w in one step
(Video 00:00–05:00 writes in matrix form, 05:00–08:01 explains convexity and the zero gradient, 08:01–12:52 computes the gradient, 12:52–17:35 solves the equation and introduces the pseudo-inverse, 17:35–18:51 gives the complete algorithm, and Fun Time follows.)
Writing E_in in matrix form
The formula for carries a running index . Page 7 packs it into matrices in four steps, and in the lecture Lin says the only purpose is to make the notation cleaner later.
Step 1: swap the order of the inner product. is the inner product of two vectors, and an inner product is commutative, so it equals :
Step 2: a sum of squares is the squared length of a vector. Stack the quantities being squared into an -dimensional vector; its squared length is the sum of the squares of its components:
Step 3: factor out the common . Every row contains . Stack row by row into a matrix and into a vector. The lecture's picture is that the originally "tall" vector lies down to become a row.
Step 4: give them names. Call the matrix and the vector :
The handout marks the sizes underneath: is , one row per example and one column per feature; is ; is . and are both given by the data, so the only thing in the formula that can move is .
The condition at the lowest point: zero gradient
Page 8 first states three properties of the function : continuous, differentiable, convex. For convexity the lecture gives only a physical picture: the function looks like a valley, like a bowl, and the left side of the slide draws exactly that, a curve opening upward with on the horizontal axis and a black dot at the lowest point.
What characterizes the bottom of the bowl? The lecture uses a ball: at the bottom of the valley, the ball cannot roll down in any direction. Each component of the gradient is the slope of the function along one direction, so the gradient at the bottom must be the zero vector:
The handout writes this as the necessary condition of the 'best' , with the note: not possible to 'roll down'. The task therefore becomes: find such that . The lecture makes a point of explaining the subscript: LIN stands for linear regression and has nothing to do with the lecturer's surname.
This article adds two points. First, the handout only says this is a necessary condition; for a convex function, a point with zero gradient is always a lowest point, so the we solve for really is the optimal solution. The lecture says no rigorous proof is given here. Second, why it is convex: for any direction , , so the quadratic term always curves upward.
Computing the gradient: one variable first, then a vector
Page 9 begins by expanding the squared length. , so
The two middle terms are both scalars and are transposes of each other, so they are equal and combine into . In the lecture, Lin compares this to the in the expansion of . The handout gives temporary names to the three quantities determined by the data:
is a matrix, is a -dimensional vector, and is a number. The three terms are quadratic, linear, and constant in .
In the lecture, Lin says many people get nervous at this point, feeling they never learned vector calculus properly. The handout's approach is to look at the case of a single first and then match it to the vector case:
| One only | Vector | |
|---|---|---|
| Handout's note | simple! | similar (derived by definition) |
The left side is the high-school derivative of a quadratic in one variable. The right side looks identical, except that becomes a matrix and becomes a vector. The handout writes "derived by definition" and lists no steps; the lecture says working it out from the definition gives the result, and that the derivation uses the fact that is a symmetric matrix. The derivation below is an addition in this article, done from the definition.
Take the partial derivative with respect to the -th component . In the linear term , only the term contains , and its derivative is . In the quadratic term , appears in the terms with and also in the terms with :
(The term with is , whose derivative is ; it appears exactly once in each of the two sums, so nothing is counted twice.) The transpose of is , so it is symmetric, which gives , and the partial derivative of the quadratic term is . Stack and put and back in their original form:
Solving the equation: the pseudo-inverse
Page 10 sets the gradient to zero:
and can both be computed, the only unknown is , and every unknown appears to the first power. In the lecture, Lin says this is simply a system of linear equations. Two cases follow.
Invertible . The handout writes: easy! unique solution. Multiply both sides on the left by the inverse:
The block has a name, the pseudo-inverse, written . In the lecture, Lin explains why it is called a "fake" inverse: if were square and had an inverse, this block would simplify to ; when is not square, it still behaves much like an inverse. This article adds a property you can check right away: , so multiplying on the left gives the identity matrix.
The handout says this case is often the case because . The lecture's explanation is that is only , the number of examples is usually much larger than , and the matrix has enough degrees of freedom, so it is invertible most of the time. This article makes that statement more concrete: if , then , which means . So fails to be invertible only when some feature happens to be an exact linear combination of the other features on all examples. The more data there is, the harder it is for that coincidence to occur.
Singular . The handout writes "many optimal solutions": the equation has many solutions, and each of them is optimal. In this case is defined in other ways, and is still one of the solutions. In the lecture, Lin says the details of the definition are in linear algebra textbooks, and most numerical software already implements it.
Both cases are written as the same formula:
The practical suggestion on this page sits in a yellow box: use a well-implemented routine instead of computing yourself, for numerical stability when the matrix is almost-singular. The lecture mentions one more benefit: you do not have to decide for yourself whether the matrix is invertible.
The complete algorithm
Page 11 wraps the whole thing into three steps:
- From the data , construct the input matrix () and the output vector ().
- Calculate the pseudo-inverse ().
- Return ().
The handout's verdict is: simple and efficient with good routine. In the lecture, Lin says the first step takes almost no work, since it only puts the inputs and outputs of the data in separate places; and that some people have only met regression in one or two dimensions, whereas the derivation here uses vectors and matrices throughout, so the same procedure works in any number of dimensions.
Numerical example: four customers, the whole algorithm by hand
The following is an example made up for this article, not from the slides. There are four customers, the only feature is annual income (in units of NTD 1 million), and the quantity to predict is the credit limit (in units of NTD 100,000):
| Customer | Annual income (NTD million) | Credit limit (NTD 100,000) |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 3 | 5 |
| 4 | 4 | 6 |
Step 1: build and . Prepend to every example. Here and :
Step 2: compute , , . The entries of are , , and ; the two components of are and ; .
Step 3: solve . That is, and . The determinant of is , which is not 0, so the matrix is invertible:
If you follow the algorithm and compute the pseudo-inverse first, it is a matrix whose -th column is times :
Multiply by : the first row gives and the second row gives , the same as above.
Step 4: look at the result. The hypothesis is .
| Customer | Residual | Squared residual | ||
|---|---|---|---|---|
| 1 | 2 | 1.9 | 0.1 | 0.01 |
| 2 | 3 | 3.3 | −0.3 | 0.09 |
| 3 | 5 | 4.7 | 0.3 | 0.09 |
| 4 | 6 | 6.1 | −0.1 | 0.01 |
Compute it once more with the formula in , , : , , and , which agrees.
What this result means. means that each additional NTD 1 million of annual income raises the predicted limit by NTD 140,000; is a base limit of NTD 50,000. The unit of is "(NTD 100,000) squared"; its square root is about 0.224, so on average the prediction is off from the actual value by a little over NTD 20,000. Comparing against a few other choices of shows that the one with zero gradient is indeed the lowest:
The singular case. Add one redundant feature: (the same annual income, now in units of NTD 500,000). The third column of is twice the second, and the determinant of is 0. Now , and as long as and , the predictions are exactly the same as before and is 0.05 in every case. , , and are all optimal solutions, which is what the handout means by many optimal solutions.
Running it in NumPy: np.linalg.inv(X.T @ X) raises a Singular matrix error outright; np.linalg.pinv(X) @ y returns , which satisfies and is one of that whole line of optimal solutions. The squared lengths of the four solutions are , , , and , and pinv gives the shortest one. This is a property of the Moore-Penrose pseudo-inverse; the handout does not mention it, and it is an addition in this article.
Fun Time: the matrix formula for the prediction vector
Question (page 12): After getting , we can calculate the predictions . If all are collected in a vector similar to how we form , what is its matrix formula?
Answer: 3. The handout's solution: , and substituting gives . The -th component of is , which is exactly the prediction for the -th example.
The analysis of the other options is this article's. Option 1 says the predictions hit every label exactly, which is true only when all residuals are 0; in the four-customer example, is not equal to . Option 2 wrongly replaces with , dropping ; on the same example it comes out as . Option 4, because , simplifies to the same formula as option 2.
After giving the answer, the lecture adds a reminder: only one matrix, , stands between and , and the analysis in the next section uses it throughout.
Does it count as learning without step-by-step improvement: the hat matrix and the learning curve
(Video 00:00–03:50 discusses whether this counts as learning, 03:50–08:25 covers the average and the hat matrix, 08:25–12:45 the geometric picture and the trace, 12:45–16:52 the noise argument and , 16:52–19:12 the learning curve, and Fun Time follows.)
An algorithm that gets there in one step
The lecture begins by asking the question on the students' behalf: is this really machine learning? The algorithm computes the answer all at once, as if it reached the top in a single step, and no learning process is visible. Page 13 sets the two sides next to each other:
| No! | Yes! |
|---|---|
| The solution is analytic (closed-form), 'instantaneous' | Good ? Yes, optimal |
| Not improving nor iteratively | Good ? Yes, finite like perceptrons |
| Improving iteratively? Somewhat, within an iterative pseudo-inverse routine |
The second row on the right rests on Foundations Lecture 7: The VC Dimension: the of -dimensional perceptrons is , which is finite. In the lecture, Lin says the model has only variables, and with enough data a good carries over into a good . For the third row, the handout says the pseudo-inverse routine is iterative inside; the example in the lecture is Gaussian elimination for computing an inverse, which is itself a loop, and if you laid out the intermediate steps you would see getting better step by step as well. The routine is just packaged so well that you do not see it.
The handout's conclusion is: if is good, learning 'happened'! The lecture uses an exam as the analogy: whether it studied in secret under the covers or read aloud to you step by step, if it does well on the final exam (), learning took place in between.
Another kind of guarantee: the average E_in
The VC bound can guarantee . This section gives a different kind of guarantee, under the handout's title 'Simpler-than-VC' Guarantee. It is available because linear regression has an analytic solution and the formulas can be written down. The object of the analysis is the average of (page 14):
The average is taken over data sets. The lecture reuses the earlier analogy: grab a handful of data from the bin and compute once, grab another handful and compute it again, and average over every handful you could possibly grab. The lecture also names the difference from VC: VC looks at an individual , and this looks at the average. The noise level is the strength of the noise in the data, the part that, as the previous lecture said, even the best target function cannot get right.
First write down for a single data set. Use from the previous Fun Time:
The last step writes as and factors it out. The handout calls the hat matrix, written , for the reason printed on the slide: because it puts ∧ on . In the lecture, Lin says the name comes from the statistics community: multiply by it and a hat grows on its head, turning it into , which makes it easy to remember.
The hat matrix is a projection
Page 15 draws a figure in . Note that this space has dimensions: is one vector in it, with one axis per example, which is a different thing from the earlier figures whose axes were features. There are four things in the figure:
- The pink region: the space spanned by the columns of (span of ). Each column is also an -dimensional vector.
- The purple : starts at the origin and points at a slant out of the pink region.
- The black : lies inside the pink region.
- The green dashed line : goes straight up from the tip of to the tip of .
The handout lists four statements. Taking them one at a time:
- is within the span. The lecture's explanation: times any takes the components of as coefficients and forms a linear combination of the columns of , so the result always lands in the span. Different correspond to different points in the span.
- is to be smallest, so it is perpendicular to the span. Linear regression wants as small as possible, that is, the point in the span closest to . The closest point is the one directly below, the perpendicular projection.
- projects to in the span.
- transforms to , perpendicular to the span. In the lecture, Lin describes this as taking the "remainder".
This article adds the algebraic version of statement 2. Being perpendicular to the span means having inner product 0 with every column of , which in matrix form is . Expand it: , which is exactly the equation obtained in the algorithm section by setting the gradient to zero. "The gradient is zero" and "the residual is perpendicular to the span" are two ways of stating the same condition. The four-customer example can be checked directly: the residual is , its inner product with the first column is , and its inner product with the second column is .
Why trace(I − H) is N − (d + 1)
Page 15 ends with one line: claim: . Why? :-) The trace is the sum of all the entries on the diagonal of a matrix. The lecture gives no proof, only the physical meaning, and asks everyone to write out the proof on their own: originally has degrees of freedom in the -dimensional space; the span it is projected onto has at most dimensions; and what is left after taking the remainder has at most degrees of freedom.
The proof below is an addition in this article and covers only the case where is invertible. It uses one property: the trace of a product of two matrices is unchanged when the order is swapped, , because both sides equal . Take and :
After the swap, the large matrix becomes the identity matrix, which has ones on its diagonal. Therefore
In the four-customer example, the hat matrix is
The diagonal sums to , and . Multiply the first row by : , which is .
I − H turns noise into the residual
Page 16 is titled An Illustrative 'Proof', and the quotation marks are the handout's own. The lecture also says no complete proof will be given, only the main idea inside it. The figure has two more things than the previous page: a red label inside the span, and a black dashed segment from to labeled noise.
The assumption. comes from some ideal plus noise, and lies in the span:
means there is some with , that is, the ideal target itself is linear.
Step 1: wipes out . A vector already in the span is unchanged by projection: . So , and
The lecture makes the same point from the figure: apply the remainder transform to the black dashed noise segment and you get the same green dashed line.
Step 2: compute the average squared length. The handout writes the result directly:
About the noise, the handout only writes "per-dimension 'noise level' ". The last equality actually holds only after averaging over the noise, and the lecture says so explicitly, describing it as the result of averaging over all the various messy noise. The calculation below is an addition in this article, and the assumption it needs is that the components of the noise, , are independent of each other, all with mean 0 and all with variance .
Write as . is symmetric and (the Fun Time in this section verifies this), so is symmetric too, and . Therefore
Take the expectation. When , independence together with mean 0 gives ; when , . Only the diagonal survives:
Divide by :
In the lecture, Lin describes the trace as the "energy" of : the noise starts with in each of directions, and after passing through only shares of it remain.
The formula for . The same page lists
The handout marks it "complicated!" and gives no derivation; the lecture says that it can be worked out in a similar way if you do the calculation seriously, only with more effort. What the lecture offers is intuition: is computed on the data we can see, and the fitted line leans a little toward the noise in this particular batch, which makes look a little better, and that is where subtracting comes from; looks at new data, the line has already leaned this way, and the noise in the new data may fall on the other side, so the two effects stack and the term is added.
This article adds a simplified version that can be computed in full. It corresponds to the remark in the lecture, made while discussing , that it is "a bit like another points". Suppose the test data uses the same and only redraws the noise, , independent of the training noise . The new labels are , the predictions are still , and the difference between them is :
The cross term has expectation 0 because the two batches of noise are independent and have mean 0; is computed the same way as in the passage on above. Dividing by gives . Each of the two terms has its own source: is the new data's own noise, and is the price of having been pulled off course by the old noise.
This article checks these three formulas with a NumPy simulation (, , features drawn from the standard normal distribution, a randomly drawn linear function as the target, and more than 40,000 repetitions for each ):
| Average : simulation / formula | Same , new noise: simulation / formula | Average with redrawn too: simulation | |
|---|---|---|---|
| 10 | 0.703 / 0.700 | 1.301 / 1.300 | about 1.47 |
| 20 | 0.849 / 0.850 | 1.147 / 1.150 | about 1.18 |
| 50 | 0.940 / 0.940 | 1.061 / 1.060 | about 1.06 |
| 100 | 0.971 / 0.970 | 1.031 / 1.030 | about 1.03 |
The first two columns match the formulas. Only the last column is the the handout defines (new drawn from the distribution as well): when is small it is higher than , 1.47 against 1.30 at , and from of 50 upward the two nearly coincide. So the simplified version above only shows where the "plus " term comes from and cannot stand as a proof of the general case. This column is a simulation result of this article under this particular setting (normally distributed features), not content from the handout; the handout gives no derivation for the general case.
The learning curve
Page 17 plots the two formulas, with the number of data points on the horizontal axis and Expected Error on the vertical axis:
- A horizontal line in the middle, at height .
- The red comes down from very high and approaches the horizontal line.
- The blue starts at the position marked on the horizontal axis (where its value is ), rises, and approaches the horizontal line.
This figure is called the learning curve. The handout makes two points. First, both converge to for . Second, the expected generalization error is , similar to the worst-case guarantee from VC. The lecture fills in how the two differ and how they are alike: VC describes the worst case and this describes the average; what they share is that both depend on the degrees of freedom and both shrink as grows.
One detail to watch: subtracting the two formulas on the previous page gives the gap
Both formulas on this page of the handout are multiplied by the noise level, but the line underneath reads "expected generalization error: ", the summary on page 23 reads " on average", and what is said aloud in the lecture is also roughly twice ; none of the three multiplies by the noise level. Going by the subtraction, there should be a factor of in front; the two ways of writing it agree numerically only when . When plugging in numbers, use the version with , which is how self-check question 2 is computed.
The handout's conclusion is: linear regression (LinReg): learning 'happened'! In the lecture, Lin explains that when is large enough we are already doing about as well as the noise level allows, and if there is not too much noise, learning really did happen. The lecture also takes a stance: the mathematical details matter less, and what Lin hopes people remember is the geometric and physical meaning, namely that with only points, the line leans a little toward those points.
Trying it on a scenario (made up for this article). Suppose there are 12 features, so , which is the dimension of the cpusmall data set used in the exercises below:
| Gap | |||
|---|---|---|---|
| 13 | 0 | 2 | 2 |
| 32 | 0.594 | 1.406 | 0.813 |
| 100 | 0.87 | 1.13 | 0.26 |
| 2,000 | 0.9935 | 1.0065 | 0.013 |
At , the error seen on the training data is only about 60% of the noise level while on new data it is 1.4 times the noise level, so looking at alone seriously overrates the model. At , is 0: with 13 unknowns and 13 examples every example can be hit exactly, yet the average is twice the noise level. To bring the gap below 5% of the noise level you need , that is, . These numbers assume the target really is linear plus noise, which real data will not match exactly, so they are better used for comparing the shape of the curves.
Fun Time: properties of the hat matrix
Question (page 18): Which of the following properties of is not true?
- is symmetric
- (double projection = single one)
- (double residual transform = single one)
- none of the above (all three are true)
Answer: 4. The handout's solution only says that 2 and 3 can be concluded from their physical meanings. In the lecture, Lin explains: after projecting once you are already in that space, and projecting again changes nothing; a vector whose remainder has been taken once is already perpendicular to that space, and taking the remainder again gives the same vector. Symmetry can more or less be read off the formula. The lecture also says nobody is required to prove these.
This article verifies each of the three once, in the case where is invertible:
- Symmetry: , because the inverse of a symmetric matrix is also symmetric.
- .
- .
The of the four customers is symmetric at a glance. The top-left entry of is the inner product of the first row with itself: , equal to the top-left entry of . Checking the whole matrix in NumPy, all three hold.
What justifies using the regression solution for classification
(Video 00:00–02:51 compares the two problems and proposes the approach, 02:51–05:30 compares the two errors, 05:30–09:03 connects to the VC bound and the uses, 09:03–10:00 is Fun Time, and the summary of the whole lecture follows.)
A lazy approach that looks workable
Page 19 puts the two linear models side by side:
| Linear classification | Linear regression | |
|---|---|---|
| Output space | ||
| Hypothesis | ||
| Error | ||
| Solving | NP-hard to solve in general | Efficient analytic solution |
The handout then points out that . In the lecture, Lin puts it this way: and are just two special values in the vast sea of real numbers. That suggests an idea:
- Run linear regression directly on the binary classification data , treating as the real numbers .
- Return .
The lecture explains the intuition: the that regression finds will probably give values greater than 0 where the label is and values less than 0 where it is ; and unlike PLA or pocket, it does not have to run for a long time, oscillating back and forth, since in software such as MATLAB it is two or three lines. The handout calls this a heuristic and asks: but explanation of this heuristic? In the lecture, Lin says it sounds reasonable, but that does not yet make it a mathematical reason.
The relation between the two errors
Both models first compute the score and differ only in how the error is computed afterward. Page 20 writes both errors as functions of the score:
It then draws two figures with on the horizontal axis and the error on the vertical axis, the blue line being the 0/1 error and the red line the squared error:
| Desired | Desired | |
|---|---|---|
| Blue line (0/1) | 1 when the score is below 0, 0 when it is above 0, a single step | 1 when the score is above 0, 0 when it is below 0 |
| Red line (squared) | A parabola with its lowest point at score , where the value is 0 | A parabola with its lowest point at score |
| Relation of the two lines | The red line lies above the blue line | The red line lies above the blue line |
The conclusion is written at the bottom of the page:
The lecture calls this a proof by picture. This article adds the algebraic version. Let . Because , , so
Look at the two cases separately. When the classification is correct, and a square cannot be negative, so the inequality holds. When the classification is wrong, and have different signs, , so and .
Plug in a few scores for :
| Score | |||||
|---|---|---|---|---|---|
| 1 | 1 | 0 | 0 | 0 | |
| 9 | 2.25 | 0.25 | 0 | 4 |
The rightmost column is the right half of the parabola: a score of 3 is a completely correct classification, yet its squared error is 4. The squared error only registers how far the score is from 1, whichever side it is on.
Connecting to the VC bound
Page 21 puts the inequality into the VC bound. For the same :
The first line is the VC bound, and the square root holds the term containing . The second line uses the fact that the 0/1 error of every example is no larger than its squared error, and so the same is true after averaging. The lecture's conclusion: the classification is bounded by the regression plus the same complexity term, so working hard to make the regression small is also one way of making the classification good.
The handout says it once more in the language of the previous lecture: take as , a (loose) upper bound used to approximate . Foundations Lecture 8: Noise and Error Measures said that the an algorithm actually optimizes can be chosen to be easy to solve, and this is an example. The price is stated in one line: trade bound tightness for efficiency.
The handout suggests two uses: is a useful baseline classifier, or it can serve as the initial PLA/pocket vector. In the lecture, Lin says that in practice it often does not perform too badly; and if the bound seems too loose, you can take it as and let PLA or pocket refine it from a decent starting point, which may need fewer corrections.
Numerical example: when the regression classifier goes wrong
The following is an example made up for this article, not from the slides. Keep the of the four customers and change the output to whether the card is approved: annual incomes 1 and 2 are labeled , 3 and 4 are labeled , so . was already computed earlier:
The four scores are , , , and , every sign is right, and the classification is 0. The squared errors are , , , and , and the regression is . , so the upper bound holds; the boundary is at , that is, , exactly midway between the two groups.
Now add a fifth customer: annual income 12, labeled . The data is still linearly separable, and a boundary between 2 and 3 gets everything right. Recompute:
| Annual income | Score | |||
|---|---|---|---|---|
| 1 | 0 | 0.475 | ||
| 2 | 0 | 0.705 | ||
| 3 | 1 | 1.021 | ||
| 4 | 0 | 0.740 | ||
| 12 | 0 | 0.117 |
The classification is and the regression is . The upper bound still holds, but the regression classifier gets the customer with annual income 3 wrong, even though a line that classifies everything correctly clearly exists for this data.
What this result means. The cause is the right half of the parabola. The fifth customer is far from the boundary: if the slope of the line stayed at 0.8, that customer's score would be 7.6, with squared error . To push down this one example, regression flattens the line, the boundary moves from 2.5 to about 3.07, and the customer with annual income 3 falls on the wrong side. This is what the handout means by loose: making the upper bound small does not guarantee that the 0/1 error we actually care about is minimized.
The second use the handout suggests shows its effect here. Run PLA from Foundations Lecture 2: Perceptrons and PLA on these five examples (this article's procedure: each time, scan down from the first example for the first misclassified point, correct with , then go back to the first example and scan again; a score of exactly 0 counts as a mistake). The result: starting from the zero vector takes 23 corrections to get everything right, and starting from takes 6. The counts depend on the order in which mistakes are looked for, and a different visiting scheme gives different numbers; this is an observation on one data set, not a guarantee.
Fun Time: which functions are upper bounds of the 0/1 error
Question (page 22): Which of the following functions are upper bounds of the pointwise 0/1 error for ?
- all of the above
Answer: 4. The handout's solution is to plot the curves and you'll see; it adds that all three can be used for binary classification, that each connects to a very important algorithm in machine learning, and that one of them will be discussed in the next lecture.
This article adds a verification that needs no plot. Let ; a correct classification is and a wrong one is . None of the three functions is ever negative, so the bound always holds when the classification is correct (0/1 error 0); all that needs checking is whether each is at least 1 when :
- : when , and .
- : when , .
- : when , , and after it is at least 1.
| 0/1 error | 1 | 1 | 1 | 0 | 0 |
| 7.389 | 2.718 | 1 | 0.368 | 0.135 | |
| 3 | 2 | 1 | 0 | 0 | |
| 3.069 | 1.895 | 1 | 0.452 | 0.183 |
The base 2 in the third function is there for a reason: with the natural logarithm, the value at would be , which is less than 1, and it would no longer be an upper bound. The one the handout promises for the next lecture is the third: the error function of Foundations Lecture 10: Logistic Regression is . Which algorithms the other two correspond to is not written in this handout; the lecture only says that after finishing this course and the one that follows (Machine Learning Techniques), you will have learned all three. What follows is an addition in this article, checked against the Techniques handouts: is written in the handout of Techniques Lecture 5: Kernel Logistic Regression and called the hinge error measure, the error that appears once soft-margin SVM is rewritten in unconstrained form; is written in the handout of Techniques Lecture 11: Gradient Boosted Decision Trees and called the exponential error measure, and that lecture explains that AdaBoost can be viewed as minimizing this error. Both handouts also note that these are upper bounds of .
The summary on page 23 condenses each of the four sections into one line: use hyperplanes to approximate real values; analytic solution with pseudo-inverse; on average; 0/1 error ≤ squared error. The preview at the bottom of the page is "binary classification, regression, and then?" In the lecture, Lin says the next lecture still uses a linear model but solves a different kind of problem.
Self-check
1. The hypothesis has no constant term, , with a single parameter. The data is , , . Find , the three predictions, and .
Answer: has a single column , , , and . The predictions are , the residuals are , and . Check: the inner product of the residuals with is , so they are perpendicular. The general formula is .
2. A regression problem has 4 features (5 weights in total with the constant term), noise level , and examples. By the handout's two formulas, what are the average , the average , and the gap between them? How much data is needed to halve the gap?
Answer: . The average , the average , and the gap is 2. The gap is , and bringing it to 1 requires .
3. When and is an invertible square matrix, what is ? What is ? Is this consistent with the handout's formula? Can we say the learning went well in this case?
Answer: when is invertible, , so , , and . The formula gives , which is consistent; matches as well. We cannot say the learning went well: the same pair of formulas gives an average of , and only because there are as many unknowns as examples, so even the noise has been fitted.
4. Take as the new labels and run linear regression again on the same . What are the resulting weights and ? Which property of the hat matrix does this correspond to?
Answer: the new weights are (using ), the same as before. The new predictions are , identical to the new labels, so . The corresponding property is : is already in the span, and projecting it again does not move it.
5. Use linear regression for binary classification. For one example with , three different give it scores , , and . What are the 0/1 error and the squared error in each case? Which one does regression most want to fix? What does this show?
Rubric: the three pairs are , , and . The largest squared error belongs to the score 3, a case that is classified completely correctly. The answer should say that the squared error penalizes "how far from ", and a score that is too large is penalized too; so minimizing the regression may sacrifice points near the boundary in order to pull back correct points that are far away. This is where the upper bound is loose, and it is what happened in the five-customer example.
Exercises and corresponding reading
How the course versions line up. On the Fall 2024 course page the slides for this lecture are 09u, which has only three subsections and drops the MOOC version's Linear Regression for Binary Classification; Fall 2024's Lecture 11 (11u) in turn opens with Linear Models for Binary Classification. This article still follows the MOOC version and covers all four sections. The Fall 2026 course page schedules Lectures 9 and 10 in W5 (10/07); when checked on 2026-09-30, that week's 09u slides were not yet public.
Textbook. According to the Fall 2024 course page, this lecture corresponds to Section 3.2 of Learning from Data (LFD).
How far self-study can go. The videos and handouts are public; the Fall 2024 homework PDFs and the LIBSVM data sets are public too, so you can work the problems yourself, but there are no official solutions and grading is for enrolled students only.
Homework. The problems in Fall 2024 HW3 (released 2024-10-07, due 10-21) related to this lecture:
- Q2: one-dimensional linear regression without , ; what is the optimal . Self-check question 1 is a numerical version of this form.
- Q3: which operation on (scaling one row, scaling one column, multiplying everything by 2, adding several columns to the first column) may change the hat matrix . Hint: is the projection onto the span, so first think about whether each operation changes the span.
- Q8: change of every example from 1 to 1126 and run linear regression again; prove that the new and old solutions differ by a diagonal matrix .
- Q10–12 (programming): use the cpusmall_scale data set from the LIBSVM site (8,192 examples, 12 features). Q10 takes , runs linear regression 1,126 times, and draws a scatter plot of ; Q11 averages 16 runs for each of and draws the learning curve; Q12 repeats this with only the first 2 features and compares with Q11.
How to do them. For the programming problems, first write np.linalg.pinv(X) @ y yourself, then cross-check the result with np.linalg.lstsq or scikit-learn's LinearRegression. Compare the shape of the learning curve from Q11 with the table in the learning-curve section; Q12 has , so by the formula the gap is much smaller, but whether the horizontal line that and converge to gets higher is the thing worth looking at. You can also redo this article's singular experiment: generate random data with and , compare pinv with computing inv(X.T @ X) @ X.T @ y yourself, then change one column into twice another column and see which one breaks. The guide to the whole assignment is in Foundations Homework.
Further reading. Stanford CS229 Lecture 2: The Supervised Learning Setup and Linear Regression covers the same problem along two routes, gradient descent and the normal equation, and can be read alongside this lecture's treatment.
Next lecture. Foundations Lecture 10: Logistic Regression changes the output to a probability and the error function to the third option of the last Fun Time. It has no analytic solution: the zero-gradient equation cannot be solved, so it has to switch to gradient descent, which walks downhill one step at a time.
Course video sources
Original videos: Linear Regression Problem, Linear Regression Algorithm, Generalization Issue, Linear Regression for Binary Classification
Checked: 2026-10-13. The video IDs, titles, and lengths were taken from the official playlist that day; the videos have no caption track on YouTube, and what this article reports from the lecture comes from transcripts made for it.
Update Log
- 2026-10-13: Previously combined with Lecture 10 in one article; changed to one article per lecture and rewritten from the handout and the video transcripts.
- 2026-10-10: Rechecked the video status. The embedded videos match the lectures in the official playlist.
- 2026-10-10: Marked the video status and verified the recording sources and how they were obtained.
References
- Foundations Lecture 9 handout, 09_handout.pdf: the main source for this article. The derivations in the four sections, the figures, and the four Fun Time questions all come from this handout.
- Machine Learning Foundations YouTube playlist: the IDs and titles of the four videos; what the text reports as said "in the lecture" is paraphrased from machine transcripts of these four videos.
- Hsuan-Tien Lin > MOOCs: the official entry for handouts and recordings.
- Techniques Lecture 5 handout, 205_handout.pdf and Techniques Lecture 11 handout, 211_handout.pdf: the algorithms that the hinge error and the exponential error in the last Fun Time each correspond to.
- Fall 2024 Lecture 9 slides (09u): the version difference, with only three subsections.
- Machine Learning, Fall 2024 course page: the corresponding LFD section and the entry to the homework.
- Machine Learning, Fall 2026 course page: the week this lecture falls in for Fall 2026.
- Fall 2024 Homework 3 (hw3_red.pdf): the content of Q2, Q3, Q8, and Q10–12.
- LIBSVM Data: cpusmall_scale: the data set for the HW3 programming problems; this article uses its number of features as the learning-curve scenario.
- Learning from Data textbook website: the source of Section 3.2.
- MOOC slide errata: the official errata entry.
- Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques: Course Overview: where this lecture sits in the whole course and the reading routes.