Home / Study together / Weeks 5-8 together
MLF + MLT + MLP: weeks 5-8 together
Weeks 5 to 8 (with a week-4 bridge): MLF tools, MLT algorithms and MLP code in the best order.
The big idea of weeks 5 to 8
In weeks 1 to 4 you built the tools (vectors, derivatives, projection, eigenvectors) and the first unsupervised algorithms. Weeks 5 to 8 are the second chapter: now we have labels, and we learn to predict. The same tools come back wearing new names:
- least squares is a projection (MLF 3) solved with a symmetric matrix (MLF 5);
- ridge is that same solution made safe, and SVD (MLF 6) shows exactly how it shrinks;
- gradient descent (MLF 7) is how a computer walks to the answer, and convexity (MLF 8) is why it always arrives;
- k-NN, trees and Naive Bayes (MLT 7, 8) are the first classifiers, and Naive Bayes needs Bayes' rule from MLF's probability weeks.
MLP then makes you type all of it in scikit-learn.
Where this sits in the exams. Quiz 2 is taken to cover weeks 5 to 8 in each of the three courses (the official pages do not print the quiz boundary, so this split is inferred from the question topics). Do the lessons in the braided order below and you prepare all three Quiz 2 papers at once. Week 4 (eigenvalues, EM) is the bridge: skim MLF week 4 first if eigenvalues feel rusty.
Dependency map (the tool, and where it is used next)
| MLF idea (the tool) | Where weeks 5 to 8 of MLT and MLP use it |
|---|---|
| W3 projection, normal equations | MLT W5 least squares is this; MLP W3 LinearRegression solves it |
| W5 symmetric matrices, real eigenvalues, PSD | is symmetric PSD, so least squares has a solid solution; MLT W6 adds to push every eigenvalue up |
| W6 SVD and low-rank | MLT W6 shows ridge shrinks each SVD direction by ; MLP W4 Ridge/Lasso |
| W7 gradient descent | MLP W3 SGDRegressor (batch and stochastic); MLP W5 logistic regression trained by GD |
| W8 convexity | least squares and ridge are convex, so one minimum and GD cannot get stuck; LASSO is convex but has a corner (MLT W6) |
| W10 Bayes rule (borrowed early) | MLT W8 Naive Bayes; MLP W6 probabilities and thresholds |
| MLT W7 k-NN and trees | MLP W9 KNeighbors..., DecisionTree... (the code comes later) |
The braided plan
Sixteen sessions. The MLP sessions are short: run the code and compare with the hand calculation from the session before.
| # | Session | Read | Why now | Time |
|---|---|---|---|---|
| 1 | Symmetric matrices | MLF week 5 | and covariance matrices are symmetric; this explains why they behave | 80 min |
| 2 | SVD and PCA | MLF week 6 | one tool that explains PCA, ridge and low-rank | 90 min |
| 3 | Least squares as regression | MLT week 5 | MLF 3's projection, now with data and noise, plus its Bayesian reading | 90 min |
| 4 | Linear regression in code | MLP week 3 (first half) | type what you just solved by hand | 45 min |
| 5 | Gradient descent | MLF week 7 | the computer's way to the same answer | 80 min |
| 6 | GD and SGD in sklearn | MLP week 3 (second half) | eta0, learning_rate, batch vs stochastic | 45 min |
| 7 | Convexity | MLF week 8 | tells you when GD is guaranteed to win | 80 min |
| 8 | Ridge and LASSO | MLT week 6 | uses sessions 1, 2, 5 and 7 together | 90 min |
| 9 | Polynomial and regularised models | MLP week 4 | PolynomialFeatures, Ridge, Lasso, direction of alpha | 50 min |
| 10 | Bayes rule, borrowed early | MLF week 10 (conditional probability and Bayes only) | Naive Bayes needs exactly this | 40 min |
| 11 | k-NN and decision trees | MLT week 7 | distance (MLF 1) as a classifier; entropy as a splitting rule | 80 min |
| 12 | Naive Bayes | MLT week 8 | Bayes rule plus an independence assumption | 80 min |
| 13 | Logistic regression in code | MLP week 5 | LogisticRegression, C, probabilities | 45 min |
| 14 | Binary classifier metrics | MLP week 6 | confusion matrix, precision, recall, PR curve | 60 min |
| 15 | Multiclass | MLP week 7 | OvR needs classifiers, OvO needs | 45 min |
| 16 | SVMs in sklearn | MLP week 8 | SVC, kernel, C, gamma (the theory is MLT week 10, later) | 50 min |
Do each lesson's quick quiz and real exam questions straight after. Tick Mark this week as done so the home page tracks you.
Five braided problems (solve them, then check)
Data: and , one feature, no intercept. (a) Find the least-squares weight . (b) Find the ridge weight with using . (c) Which is smaller, and why?
Need a hint?
Here is a single number, .
Show the full solution
and . (a) . (b) . (c) The ridge weight is smaller: adding to the denominator shrinks the weight towards zero. That is the whole idea of ridge: a slightly worse fit on the training data, in return for a steadier weight.
Two features are exact copies, so is replaced in a bad case by . (a) Is invertible? Give its eigenvalues. (b) Find the eigenvalues and determinant of for .
Need a hint?
For a symmetric matrix the eigenvalues add when you add .
Show the full solution
(a) , so is not invertible. Trace and determinant give eigenvalues and . (b) with determinant . Its eigenvalues are and (product ✓). Adding lifts every eigenvalue by , so none can be . That is why ridge works even when features are copies of each other.
In the SVD of , ridge multiplies the least-squares answer along a direction with singular value by . For , compute the factor for and for . Which direction is shrunk more?
Need a hint?
Plug in. Compare with 1 (no shrinkage).
Show the full solution
: . : . The small-singular-value direction is shrunk far more. Small means the data barely varies in that direction, so the weight there is the least trustworthy: ridge quietly distrusts it. Least squares has factor everywhere.
For the data of Problem 1, the loss is . (a) Find and and say why GD is guaranteed to find the minimum. (b) Do one GD step from with . (c) What is the largest that still converges?
Need a hint?
everywhere means convex.
Show the full solution
(a) , everywhere, so is convex with one minimum, at : , the same answer as before. (b) . (c) For a quadratic with curvature , GD converges when . In scikit-learn this is exactly why too large an eta0 makes SGDRegressor blow up.
. The word "free" appears in of spam and of non-spam. An email contains "free". Find .
Need a hint?
Bayes: posterior likelihood prior, divided by the total.
Show the full solution
Numerator: . Other branch: . So . Naive Bayes does this once per word and multiplies the likelihoods, pretending the words are independent.
What the weeks become in each other (cheat-sheet)
| Idea | In MLF | In MLT | In MLP |
|---|---|---|---|
| Normal equations | W3 projection | W5 least squares | LinearRegression |
| Adding | W5 eigenvalue shift | W6 ridge | Ridge(alpha=...) |
| Sparse solutions | W8 convex, non-smooth | W6 LASSO | Lasso(alpha=...) |
| Walk downhill | W7 gradient descent | W5 to W6 iterative solvers | SGDRegressor(eta0, learning_rate) |
| Probability | W10 Bayes | W8 Naive Bayes | GaussianNB, predict_proba |
| Many classes | - | W9 to W10 | W7 OvR , OvO |
Counting skills that pay every time in Quiz 2. Number of fits in a grid search combinations folds ( refit). OvR trains models, OvO trains . Larger ridge/LASSO penalty smaller weights: alpha up, C down is the stronger regularisation. Learn these as three one-line rules.
Short on time?
4-day crash version (Quiz 2 only). Day 1: MLF 5 and 6 (focus on PSD and SVD facts). Day 2: MLT 5 and 6 plus MLP 3 and 4 (the ridge story). Day 3: MLF 7 and 8 plus MLP 5 and 6 (GD, convexity, metrics). Day 4: MLT 7 and 8 plus MLP 7 and 8 (classifiers). Finish every day with that week's real exam questions.
After the sessions, sit one full Quiz 2 paper per course from Past papers under a timer. Then continue to weeks 1 to 8 together or weeks 1 to 12 together.